-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.lua
66 lines (51 loc) · 2.02 KB
/
io.lua
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
-- MODULE IO.LUA
-- by Garran Plum
--
-- Foundation-specific I/O functions for all GP mods.
--
-- FUNCTION ASSIGNMENTS
-- IMPORT GP OBJECT
local myMod, GP = ...
-- GP FOUNDATION FUNCTION Write
-- Writes a string to a file. Default = "GP.log".
-- I/O EFFECT
function GP:write(fileContent, fileName)
-- Set a default fileName if one isn't provided.
fileName = fileName or "GP.log"
-- Setup for return status.
local isWriteSuccessful = false
-- Setup file path.
local filePath = GP:magicWords().log.folder .. '/' .. fileName
-- Clear a path to writing by ensuring folders exist.
GP:clearPath(filePath)
-- Call the Foundation function to write the file and grab the return boolean.
isWriteSuccessful = myMod:writeFileAsString(filePath, fileContent)
end
-- GP FOUNDATION FUNCTION Write Table
-- Writes a table to a file. Default = [modName].log
-- I/O EFFECT
function GP:writeTable(incomingTable, fileName)
-- Set a default fileName if one isn't provided.
-- Uses GP.load instead of GP:config() to preclude looping if config() calls writeTable().
fileName = (fileName or GP:load(configPath).modName) .. ".log"
-- Registration w/ DataType? Add a folder for it.
if incomingTable.DataType then
fileName = incomingTable.DataType .. "/" .. fileName
end
-- Add a local var declaration to make the log valid Lua.
local logVar = incomingTable.Id or "gpsLog"
local varDeclaration = "local" .. " " .. logVar .. " = " .. "\n"
-- Write the table serialized as a string with the log variable declaration.
GP:write(varDeclaration .. GP:serializeTable(incomingTable), fileName)
end
-- GP FOUNDATION FUNCTION Clear Path
-- Clears a path to file writing by creating all nested folders.
-- I/O EFFECT
function GP:clearPath(incomingString)
-- Separate the file path and file name.
local filePath, fileName = GP:filePath(incomingString)
-- If path doesn't exist, create it.
if not myMod:directoryExists(filePath) then
myMod:createDirectory(filePath)
end
end