SK = SK or {}local Xmtool = {}--[[@brief Defined some commonly used functions @by xiaoming]]local countcode = [[ Local i = 0 return function () i = i + 1 return I end]]---custom counter xmtool.x Mcounter = Assert (LoadString (countcode))-----------------------------------get a singleton function for a class [email protected] Clss To obtain the Singleton class [email protected] singleton the Singleton [email protected] that needs to be acquired CLSS Singleton---------------------------------function Xmtool.getsingleton (Clss,singleton,...) Return singleton or clss:new (...) End-----------------------------------------Wraps the callback function so that the class method defines the object of the function that [email protected] obj will call [email Protected] Method function to invoke---------------------------------------functions Xmtool.handlercall (obj, method) return function (...) Return method (obj, ...) EndEnd---------------------------------------------string split [email protected] input string [email protected ] delimiter the character used for the split [email protected] #table/If the delimiterIs null, it returns false-------------------------------------------function xmtool.splitstr (input, delimiter) input = ToString ( Input) delimiter = ToString (delimiter) if (delimiter== ") then return False End local Pos,arr = 0, {}--for E Ach divider found for st,sp in function () return string.find (Input, delimiter, POS, true) end do Table.insert (AR R, String.sub (input, POS, st-1)) pos = sp + 1 End Table.insert (arr, string.sub (input, POS)) return Arren d-------------------------------------string split [email protected] input string [email protected] Delimiter The character used for the split [email protected] #string-----------------------------------function xmtool.getsplitstr (input, delimiter) Local arr = Xmtool.splitstr (input,delimiter) Local str = "" if (Type (arr) ~= "table") then return TOSTR ing (input) else for i=1, #arr do str = str. Arr[i] End end return strend-------------------------------printing information-----------------------------function xmtool.sklOG (...) Print (String.Format (...)) End------------------------------Print log--------------------------------function xmtool.printfuncmsg (Msginfo, Tablefunc) Printinfo (msginfo., file:%s,line:%s ", Tablefunc.source,tablefunc.currentline) End-------------------------------Print the appropriate stack information--msg the custom information we added-----------------------------function Xmtool.printtracemsg (msg) xmtool.sklog ("----------------------------------------") Xmtool.sklog ("msg:"). ToString (msg): "\ n") Xmtool.sklog (Debug.traceback ()) Xmtool.sklog ("----------------------------------------") return msgend----- -----------------------------------critical function error detection [email protected] func #function function to be detected [email protected] msg custom information [email protected] level error levels--------------------------------------function Xmtool.catcherror (func,msg, Level) local state, message = Xpcall (Func,function () return xmtool.printtracemsg (msg) end) return state or error (Me Ssage,level) End-----------------------------------Convert a number to a percent string [email&Nbsp;protected] Number of digits to convert [email protected] retain number of decimal digits to retain---------------------------------function Xmtool.getpercentstring (number,retain) Local ret = ToString (retain) return String.Format ("%.": Ret.. " F ".." Percent ", number*100) end
Some of the most commonly used tool functions of the LUA customization