-
Notifications
You must be signed in to change notification settings - Fork 1
/
ME5_Common.lua
154 lines (136 loc) · 4.1 KB
/
ME5_Common.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
-- **************************************************************
-- MASS EFFECT: UNIFICATION Common Function Library
-- Aaron Gilbert - [email protected]
-- Copyright (c) 2021 Aaron Gilbert. All rights reserved.
--
-- About:
-- <script description>
--
-- Legal:
-- REPRODUCTION OR TRANSMISSION OF ALL OR PART OF THIS COPYRIGHT WORK IN ANY MEDIUM OR BY ELECTRONIC MEANS WITHOUT THE WRITTEN PERMISSION OF AARON GILBERT IS PROHIBITED.
-- THIS SCRIPT IS NOT MADE, DISTRIBUTED, OR SUPPORTED BY LUCASARTS, A DIVISION OF LUCASFILM ENTERTAINMENT COMPANY LTD.
-- **************************************************************
local __SCRIPT_NAME = "ME5_Common";
local debug = true
local function PrintLog(...)
if debug == true then
print("["..__SCRIPT_NAME.."]", unpack(arg));
end
end
local _;
local pairs = pairs;
local ipairs = ipairs;
-- ********************************
-- DEBUG FUNCTIONS
-- ********************************
Debug = {};
-- If condition is false, prints a message to the debug log
function Debug.Warning(condition, ...)
if not condition then
print("[Warning]", unpack(arg));
return false;
else
return true;
end
end
-- `assert` but with '[Assert]' prepended to the message
function Debug.Assert(condition, ...)
arg[1] = "[Assert]\t"..arg[1];
return assert(condition, unpack(arg));
end
-- ********************************
-- MISC FUNCTIONS
-- ********************************
-- Gets the length of the specified table or string
function getn(v)
local v_type = type(v);
if v_type == "table" then
return table.getn(v);
elseif v_type == "string" then
return string.len(v);
else
return;
end
end
-- Returns true if the specified number is between the specified min and max
function IsBetween(v, min, max)
local v_type = type(v);
if v_type == "number" then
return v >= min and v <= max;
else
return;
end
end
-- ********************************
-- STRING FUNCTIONS
-- ********************************
-- If string begins with another string
function string.starts(str, Start)
return string.sub(str, 1, getn(Start)) == Start;
end
-- If string ends with another string
function string.ends(str, End)
return End == "" or string.sub(str, -getn(End)) == End;
end
-- Splits string by one or more delimiter characters into a table
function string.split(source, delimiters)
local elements = {};
local pattern = "([^"..delimiters.."]+)";
string.gsub(source, pattern, function(value) elements[getn(elements) + 1] = value; end);
return elements;
end
-- Lua implementation of C#'s String.Format method. Token indexes must be 1-based.
--
-- Usage:
-- string.formatcs("first = {1}, second = {2}, third = {3}", "value one", "value two", "value three")
--
-- This example would return the following string:
-- first = value one, second = value two, three = value three
--
-- Further documentation: https://msdn.microsoft.com/en-us/library/system.string.format
function string.formatcs(str, ...)
if getn(arg) > 0 then
for k,v in ipairs(arg) do
str = string.gsub(str, "{"..(k).."}", tostring(arg[k]));
end
end
return str;
end
-- ********************************
-- MATH FUNCTIONS
-- ********************************
-- Clamps value between low and high range values
function math.clamp(n, low, high)
if n < low then
return low
elseif n > high then
return high
else
return n
end
end
-- ********************************
-- TABLE FUNCTIONS
-- ********************************
-- Print contents of `tbl`, with indentation.
-- `indent` sets the initial level of indentation.
-- Derived from https://gist.github.com/hashmal/874792 and https://gist.github.com/xytis/5361405
function tprint(tbl, indent)
if not indent then
indent = 1
print(tostring(tbl))
end
if tbl then
for k, v in pairs(tbl) do
if not string.starts(tostring(k), "__") then
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
tprint(v, indent + 1)
else
print(formatting .. tostring(v))
end
end
end
end
end