File: cfile_include.lua

package info (click to toggle)
freespace2 25.0.0~rc11%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (78 lines) | stat: -rw-r--r-- 3,230 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- A version of include for lua. Originally written by Nuke

local chunck_cache = {}
--- function to load and run lua files
-- takes string filename and boolean cacheonly
-- cfile will try to find filename in all accesible locations and load it only if it hasnt done so already
-- if cacheonly flag is set, code will be cached but not executed, good for inits. however it is ignored when calling an already cached chunk.
-- returns true on success or nil on failure to find/load file
local function execute_lua_file(filename, cacheonly)
    --dont reload chunks from file if they have already been loaded. because faster == better
    if (type(chunck_cache[filename]) == "table" and type(chunck_cache[filename][1]) == "function") then
        if (not chunck_cache[filename][2] and not chunck_cache[filename][3]) then
            chunck_cache[filename][2] = true
            local val, err = pcall(chunck_cache[filename][1])
            if (err) then
                -- Add an boolean to indicate that this function has caused an error
                chunck_cache[filename][3] = true

                return false, string.format("Error while executing external lua file %q:\n%s", filename, err)
            end
            return true
        else
            return false
        end
    else
        if (cf.fileExists(filename, "", true)) then
            --open the file
            local file = cf.openFile(filename, "r", "")
            local fstring = file:read("*a") --load it all into a string
            file:close()
            if (fstring and type(fstring) == "string") then
                --use the string as a code chunk and convert it to function
                local func, error = loadstring(fstring, filename)
                if (not func) then -- Compile error
                    return false, string.format("Error while processing file %q. Errormessage:\n%s", filename, error)
                end
                chunck_cache[filename] = {}
                chunck_cache[filename][1] = func
                chunck_cache[filename][2] = false
                --maybe execute
                if (not cacheonly) then
                    chunck_cache[filename][2] = true
                    local val, err = xpcall(chunck_cache[filename][1], function(err)
                    -- Add an boolean to indicate that this function has caused an error
                        chunck_cache[filename][3] = true

                        return string.format("Error while executing external lua file %q:\n%s\n\n%s", filename, err, debug.traceback())
                    end)

                    if (err) then
                        return false, err
                    end
                end
                return true
            end
        else
            return false, string.format("Couldn't find external lua file %q!", filename)
        end
    end
end



function include(fileName)
    if (fileName == nil) then
        stackErrorf("Invalid argument for 'include'!")
    else
        if (not fileName:ends(".lua")) then
             fileName = fileName .. ".lua"
        end

        local succ, err = execute_lua_file(fileName)

        if (not succ and err) then
           error(err)
        end
    end
end