summaryrefslogtreecommitdiff
path: root/scripts/granger
diff options
context:
space:
mode:
authorIronClawTrem <louie.nutman@gmail.com>2020-02-16 03:40:06 +0000
committerIronClawTrem <louie.nutman@gmail.com>2020-02-16 03:40:06 +0000
commit425decdf7e9284d15aa726e3ae96b9942fb0e3ea (patch)
tree6c0dd7edfefff1be7b9e75fe0b3a0a85fe1595f3 /scripts/granger
parentccb0b2e4d6674a7a00c9bf491f08fc73b6898c54 (diff)
create tremded branch
Diffstat (limited to 'scripts/granger')
-rw-r--r--scripts/granger/lib/init.lua5
-rw-r--r--scripts/granger/lib/os.lua222
-rw-r--r--scripts/granger/lib/path.lua137
-rw-r--r--scripts/granger/lib/string.lua50
-rw-r--r--scripts/granger/lib/table.lua181
-rw-r--r--scripts/granger/main.lua63
-rw-r--r--scripts/granger/util.lua28
7 files changed, 686 insertions, 0 deletions
diff --git a/scripts/granger/lib/init.lua b/scripts/granger/lib/init.lua
new file mode 100644
index 0000000..9158d0b
--- /dev/null
+++ b/scripts/granger/lib/init.lua
@@ -0,0 +1,5 @@
+require "lib.os"
+require "lib.path"
+require "lib.string"
+require "lib.table"
+require "lib.log"
diff --git a/scripts/granger/lib/os.lua b/scripts/granger/lib/os.lua
new file mode 100644
index 0000000..66941a7
--- /dev/null
+++ b/scripts/granger/lib/os.lua
@@ -0,0 +1,222 @@
+--
+-- os.lua
+-- Additions to the OS namespace.
+-- Copyright (c) 2002-2011 Jason Perkins and the Premake project
+-- Copyright (c) 2015 Jeff Kent <jeff@jkent.net>
+--
+
+
+--
+-- Retrieve the current operating system ID string.
+--
+
+ function os.get()
+ return _OS
+ end
+
+
+
+--
+-- Check the current operating system.
+--
+
+ function os.is(id)
+ return (os.get():lower() == id:lower())
+ end
+
+
+
+--
+-- Determine if the current system is running a 64-bit architecture
+--
+
+ local _64BitHostTypes = {
+ "x86_64",
+ "ia64",
+ "amd64",
+ "ppc64",
+ "powerpc64",
+ "sparc64"
+ }
+
+ function os.is64bit()
+ -- Call the native code implementation. If this returns true then
+ -- we're 64-bit, otherwise do more checking locally
+ if (os._is64bit()) then
+ return true
+ end
+
+ -- Identify the system
+ local arch
+ if _OS == "windows" then
+ arch = os.getenv("PROCESSOR_ARCHITECTURE")
+ elseif _OS == "macosx" then
+ arch = os.outputof("echo $HOSTTYPE")
+ else
+ arch = os.outputof("uname -m")
+ end
+
+ -- Check our known 64-bit identifiers
+ arch = arch:lower()
+ for _, hosttype in ipairs(_64BitHostTypes) do
+ if arch:find(hosttype) then
+ return true
+ end
+ end
+ return false
+ end
+
+
+
+--
+-- The os.matchdirs() and os.matchfiles() functions
+--
+
+ local function domatch(result, mask, wantfiles)
+ -- need to remove extraneous path info from the mask to ensure a match
+ -- against the paths returned by the OS. Haven't come up with a good
+ -- way to do it yet, so will handle cases as they come up
+ if mask:startswith("./") then
+ mask = mask:sub(3)
+ end
+
+ -- strip off any leading directory information to find out
+ -- where the search should take place
+ local basedir = mask
+ local starpos = mask:find("%*")
+ if starpos then
+ basedir = basedir:sub(1, starpos - 1)
+ end
+ basedir = path.getdirectory(basedir)
+ if (basedir == ".") then basedir = "" end
+
+ -- recurse into subdirectories?
+ local recurse = mask:find("**", nil, true)
+
+ -- convert mask to a Lua pattern
+ mask = path.wildcards(mask)
+
+ local function matchwalker(basedir)
+ local wildcard = path.join(basedir, "*")
+
+ -- retrieve files from OS and test against mask
+ local m = os.matchstart(wildcard)
+ while (os.matchnext(m)) do
+ local isfile = os.matchisfile(m)
+ if ((wantfiles and isfile) or (not wantfiles and not isfile)) then
+ local basename = os.matchname(m)
+ local fullname = path.join(basedir, basename)
+ if basename ~= ".." and fullname:match(mask) == fullname then
+ table.insert(result, fullname)
+ end
+ end
+ end
+ os.matchdone(m)
+
+ -- check subdirectories
+ if recurse then
+ m = os.matchstart(wildcard)
+ while (os.matchnext(m)) do
+ if not os.matchisfile(m) then
+ local dirname = os.matchname(m)
+ if (not dirname:startswith(".")) then
+ matchwalker(path.join(basedir, dirname))
+ end
+ end
+ end
+ os.matchdone(m)
+ end
+ end
+
+ matchwalker(basedir)
+ end
+
+ function os.matchdirs(...)
+ local result = { }
+ for _, mask in ipairs(arg) do
+ domatch(result, mask, false)
+ end
+ return result
+ end
+
+ function os.matchfiles(...)
+ local result = { }
+ for _, mask in ipairs(arg) do
+ domatch(result, mask, true)
+ end
+ return result
+ end
+
+
+
+--
+-- An overload of the os.mkdir() function, which will create any missing
+-- subdirectories along the path.
+--
+
+ local builtin_mkdir = os.mkdir
+ function os.mkdir(p)
+ local dir = iif(p:startswith("/"), "/", "")
+ for part in p:gmatch("[^/]+") do
+ dir = dir .. part
+
+ if (part ~= "" and not path.isabsolute(part) and not os.isdir(dir)) then
+ local ok, err = builtin_mkdir(dir)
+ if (not ok) then
+ return nil, err
+ end
+ end
+
+ dir = dir .. "/"
+ end
+
+ return true
+ end
+
+
+--
+-- Run a shell command and return the output.
+--
+
+ function os.outputof(cmd)
+ local pipe = io.popen(cmd)
+ local result = pipe:read('*a')
+ pipe:close()
+ return result
+ end
+
+
+--
+-- Remove a directory, along with any contained files or subdirectories.
+--
+
+ local builtin_rmdir = os.rmdir
+ function os.rmdir(p)
+ -- recursively remove subdirectories
+ local dirs = os.matchdirs(p .. "/*")
+ for _, dname in ipairs(dirs) do
+ os.rmdir(dname)
+ end
+
+ -- remove any files
+ local files = os.matchfiles(p .. "/*")
+ for _, fname in ipairs(files) do
+ os.remove(fname)
+ end
+
+ -- remove this directory
+ builtin_rmdir(p)
+ end
+
+
+--
+-- Elevate and set _ELEVATED global
+--
+
+ _ENV._ELEVATED = false
+ local builtin_elevate = os.elevate
+ function os.elevate()
+ _ENV._ELEVATED = builtin_elevate()
+ return _ENV._ELEVATED
+ end
+
diff --git a/scripts/granger/lib/path.lua b/scripts/granger/lib/path.lua
new file mode 100644
index 0000000..7bf4b2b
--- /dev/null
+++ b/scripts/granger/lib/path.lua
@@ -0,0 +1,137 @@
+--
+-- path.lua
+-- Path manipulation functions.
+-- Copyright (c) 2002-2010 Jason Perkins and the Premake project
+--
+
+
+--
+-- Retrieve the filename portion of a path, without any extension.
+--
+
+ function path.getbasename(p)
+ local name = path.getname(p)
+ local i = name:findlast(".", true)
+ if (i) then
+ return name:sub(1, i - 1)
+ else
+ return name
+ end
+ end
+
+
+--
+-- Retrieve the directory portion of a path, or an empty string if
+-- the path does not include a directory.
+--
+
+ function path.getdirectory(p)
+ local i = p:findlast("/", true)
+ if (i) then
+ if i > 1 then i = i - 1 end
+ return p:sub(1, i)
+ else
+ return "."
+ end
+ end
+
+
+--
+-- Retrieve the drive letter, if a Windows path.
+--
+
+ function path.getdrive(p)
+ local ch1 = p:sub(1,1)
+ local ch2 = p:sub(2,2)
+ if ch2 == ":" then
+ return ch1
+ end
+ end
+
+
+
+--
+-- Retrieve the file extension.
+--
+
+ function path.getextension(p)
+ local i = p:findlast(".", true)
+ if (i) then
+ return p:sub(i)
+ else
+ return ""
+ end
+ end
+
+
+
+--
+-- Retrieve the filename portion of a path.
+--
+
+ function path.getname(p)
+ local i = p:findlast("[/\\]")
+ if (i) then
+ return p:sub(i + 1)
+ else
+ return p
+ end
+ end
+
+
+--
+-- Takes a path which is relative to one location and makes it relative
+-- to another location instead.
+--
+
+ function path.rebase(p, oldbase, newbase)
+ p = path.getabsolute(path.join(oldbase, p))
+ p = path.getrelative(newbase, p)
+ return p
+ end
+
+
+--
+-- Convert the separators in a path from one form to another. If `sep`
+-- is nil, then a platform-specific separator is used.
+--
+
+ local builtin_translate = path.translate
+
+ function path.translate(p, sep)
+ if not sep then
+ if os.is("windows") then
+ sep = "\\"
+ else
+ sep = "/"
+ end
+ end
+ return builtin_translate(p, sep)
+ end
+
+
+--
+-- Converts from a simple wildcard syntax, where * is "match any"
+-- and ** is "match recursive", to the corresponding Lua pattern.
+--
+-- @param pattern
+-- The wildcard pattern to convert.
+-- @returns
+-- The corresponding Lua pattern.
+--
+
+ function path.wildcards(pattern)
+ -- Escape characters that have special meanings in Lua patterns
+ pattern = pattern:gsub("([%+%.%-%^%$%(%)%%])", "%%%1")
+
+ -- Replace wildcard patterns with special placeholders so I don't
+ -- have competing star replacements to worry about
+ pattern = pattern:gsub("%*%*", "\001")
+ pattern = pattern:gsub("%*", "\002")
+
+ -- Replace the placeholders with their Lua patterns
+ pattern = pattern:gsub("\001", ".*")
+ pattern = pattern:gsub("\002", "[^/]*")
+
+ return pattern
+ end
diff --git a/scripts/granger/lib/string.lua b/scripts/granger/lib/string.lua
new file mode 100644
index 0000000..56fae4d
--- /dev/null
+++ b/scripts/granger/lib/string.lua
@@ -0,0 +1,50 @@
+--
+-- string.lua
+-- Additions to Lua's built-in string functions.
+-- Copyright (c) 2002-2008 Jason Perkins and the Premake project
+--
+
+
+--
+-- Returns an array of strings, each of which is a substring of s
+-- formed by splitting on boundaries formed by `pattern`.
+--
+
+ function string.explode(s, pattern, plain)
+ if (pattern == '') then return false end
+ local pos = 0
+ local arr = { }
+ for st,sp in function() return s:find(pattern, pos, plain) end do
+ table.insert(arr, s:sub(pos, st-1))
+ pos = sp + 1
+ end
+ table.insert(arr, s:sub(pos))
+ return arr
+ end
+
+
+
+--
+-- Find the last instance of a pattern in a string.
+--
+
+ function string.findlast(s, pattern, plain)
+ local curr = 0
+ repeat
+ local next = s:find(pattern, curr + 1, plain)
+ if (next) then curr = next end
+ until (not next)
+ if (curr > 0) then
+ return curr
+ end
+ end
+
+
+
+--
+-- Returns true if `haystack` starts with the sequence `needle`.
+--
+
+ function string.startswith(haystack, needle)
+ return (haystack:find(needle, 1, true) == 1)
+ end
diff --git a/scripts/granger/lib/table.lua b/scripts/granger/lib/table.lua
new file mode 100644
index 0000000..3076cb0
--- /dev/null
+++ b/scripts/granger/lib/table.lua
@@ -0,0 +1,181 @@
+--
+-- table.lua
+-- Additions to Lua's built-in table functions.
+-- Copyright (c) 2002-2008 Jason Perkins and the Premake project
+--
+
+
+--
+-- Returns true if the table contains the specified value.
+--
+
+ function table.contains(t, value)
+ for _,v in pairs(t) do
+ if (v == value) then
+ return true
+ end
+ end
+ return false
+ end
+
+
+--
+-- Enumerates an array of objects and returns a new table containing
+-- only the value of one particular field.
+--
+
+ function table.extract(arr, fname)
+ local result = { }
+ for _,v in ipairs(arr) do
+ table.insert(result, v[fname])
+ end
+ return result
+ end
+
+
+
+--
+-- Flattens a hierarchy of tables into a single array containing all
+-- of the values.
+--
+
+ function table.flatten(arr)
+ local result = { }
+
+ local function flatten(arr)
+ for _, v in ipairs(arr) do
+ if type(v) == "table" then
+ flatten(v)
+ else
+ table.insert(result, v)
+ end
+ end
+ end
+
+ flatten(arr)
+ return result
+ end
+
+
+--
+-- Merges an array of items into a string.
+--
+
+ function table.implode(arr, before, after, between)
+ local result = ""
+ for _,v in ipairs(arr) do
+ if (result ~= "" and between) then
+ result = result .. between
+ end
+ result = result .. before .. v .. after
+ end
+ return result
+ end
+
+
+--
+-- Inserts a value of array of values into a table. If the value is
+-- itself a table, its contents are enumerated and added instead. So
+-- these inputs give these outputs:
+--
+-- "x" -> { "x" }
+-- { "x", "y" } -> { "x", "y" }
+-- { "x", { "y" }} -> { "x", "y" }
+--
+
+ function table.insertflat(tbl, values)
+ if type(values) == "table" then
+ for _, value in ipairs(values) do
+ table.insertflat(tbl, value)
+ end
+ else
+ table.insert(tbl, values)
+ end
+ end
+
+
+--
+-- Returns true if the table is empty, and contains no indexed or keyed values.
+--
+
+ function table.isempty(t)
+ return next(t) == nil
+ end
+
+
+--
+-- Adds the values from one array to the end of another and
+-- returns the result.
+--
+
+ function table.join(...)
+ local result = { }
+ for _,t in ipairs(arg) do
+ if type(t) == "table" then
+ for _,v in ipairs(t) do
+ table.insert(result, v)
+ end
+ else
+ table.insert(result, t)
+ end
+ end
+ return result
+ end
+
+
+--
+-- Return a list of all keys used in a table.
+--
+
+ function table.keys(tbl)
+ local keys = {}
+ for k, _ in pairs(tbl) do
+ table.insert(keys, k)
+ end
+ return keys
+ end
+
+
+--
+-- Adds the key-value associations from one table into another
+-- and returns the resulting merged table.
+--
+
+ function table.merge(...)
+ local result = { }
+ for _,t in ipairs(arg) do
+ if type(t) == "table" then
+ for k,v in pairs(t) do
+ result[k] = v
+ end
+ else
+ error("invalid value")
+ end
+ end
+ return result
+ end
+
+
+
+--
+-- Translates the values contained in array, using the specified
+-- translation table, and returns the results in a new array.
+--
+
+ function table.translate(arr, translation)
+ local result = { }
+ for _, value in ipairs(arr) do
+ local tvalue
+ if type(translation) == "function" then
+ tvalue = translation(value)
+ else
+ tvalue = translation[value]
+ end
+ if (tvalue) then
+ table.insert(result, tvalue)
+ end
+ end
+ return result
+ end
+
+ \ No newline at end of file
diff --git a/scripts/granger/main.lua b/scripts/granger/main.lua
new file mode 100644
index 0000000..304f45a
--- /dev/null
+++ b/scripts/granger/main.lua
@@ -0,0 +1,63 @@
+--
+-- main.lua
+-- Granger main
+-- Copyright (c) 2016 Jeff Kent <jeff@jkent.net>
+--
+
+require 'scripts/granger/lib'
+
+local install_files = {}
+
+if os.is('windows') then
+ install_files = {
+ "tremulous.exe"
+ "tremded.exe"
+ "granger.exe"
+ "SDL264.dll"
+ "renderer_opengl1.dll"
+ "renderer_opengl2.dll"
+ }
+elseif os.is('linux') then
+ install_files = {
+ "tremulous"
+ "tremded"
+ "granger"
+ "renderer_opengl1.so"
+ "renderer_opengl2.so"
+ }
+elseif os.is('macosx') then
+ install_files = {
+ "tremulous"
+ "tremded"
+ "granger"
+ "libSDL2-2.0.0.dylib"
+ "renderer_opengl1.dylib"
+ "renderer_opengl2.dylib"
+ }
+else
+ os.exit(1)
+end
+
+local dst_dir = path.getdirectory('.')
+local dst_dir = path.getdirectory(_EXE_PATH)
+
+local privs = false
+for file in ipairs(install_files) do
+ local src = path.join(src_dir, file)
+ local dst = path.join(dst_dir, file)
+ if not os.access(dst, 'w') then
+ privs = true
+ end
+end
+
+if privs then
+ os.elevate()
+end
+
+for file in ipairs(install_files) do
+ local src = path.join(src_dir, file)
+ local dst = path.join(dst_dir, file)
+ os.rename(src, dst)
+end
+
+--- Copyright (C) 2015-2019 GrangerHub
diff --git a/scripts/granger/util.lua b/scripts/granger/util.lua
new file mode 100644
index 0000000..77dc476
--- /dev/null
+++ b/scripts/granger/util.lua
@@ -0,0 +1,28 @@
+--
+-- util.lua
+-- various utility functions
+-- Copyright (c) 2016 Jeff Kent <jeff@jkent.net>
+--
+
+local function hash_file(file, ctx)
+ local f = io.open(file, "r")
+ if f == nil then
+ return nil
+ end
+ repeat
+ local buf = f:read(0x10000)
+ ctx:update(buf)
+ until buf == nil
+ f:close()
+ return tostring(ctx)
+end
+
+function sha256_file(file)
+ local ctx = nettle.sha256()
+ return hash_file(file, ctx)
+end
+
+function md5_file(file)
+ local ctx = nettle.md5()
+ return hash_file(file, ctx)
+end