Template:TagSSSource

print("Please Wait...")

-- Variables local print = print local clear = clear local math = math local io = io

-- Files local RootPath = "" local path = "" local DirFile = "DirFile" local TerrainHiberFile = "TerrainHiberFile" local FileRunning = "Not Started" local Running = 1

ByteMulti = { [1]="Byte", [2]="Kilobyte", [3]="Megabyte", [4]="Gigabyte", [5]="Terabyte", [6]="Petabyte", [7]="Exabyte", [8]="Zettabyte", [9]="Yottabyte" } local ByteMulti = ByteMulti -- User Accounts local UserFileDir = "TerUser" local CurUser, UserPiv = "", "NotSignedIn" -- Tables -- TagSS = { }

-- SignIn local Users = {} local UsersSignInCre = {} local SignInTable = {}

--- --- Functions --- ---

-- New in Preview R2.04.3 function LowMemory Tag = "A" for p=1,26 do     Tag = Tag..Tag print(collectgarbage("count")) end end

-- New in Preview R2.01 function TagSSVersion return "PreR2.04.5" end TagSS.Version = TagSSVersion

-- New in Preview R2 function Wait(Time) local OldTime=os.clock while os.clock<(OldTime+Time) do  end return os.clock-OldTime end

--- -- User Accounts -- ---

-- Used by NUserFile function AddUserToList(UserName) local DirHandle=io.open(RootPath..UserFileDir, "a") DirHandle:write(UserName,"\n") DirHandle:close end

-- Create UserFile for Password function NUserFile(UserName, Password, Privileges, LOBlockTimes, LOBadPwT, LOMaxBlock, LOMaxBad, AUTL ) if AUTL==true then AddUserToList(UserName) end local PassFile = io.open(RootPath..UserFileDir..UserName, "w") PassFile:write(Password.."\n") PassFile:write(Privileges.."\n") PassFile:write(LOBlockTimes.. "\n"..LOBadPwT.. "\n"..LOMaxBlock.. "\n"..LOMaxBad) PassFile:close return "Ok" end

-- Signs in to your user account function SignIn(UserName,Password) local UserIsVa = UserExists(UserName) local UsFileT,CorPassword, Privileges, LOBlockTimes, LOBadPwT, LOMaxBlock, LOMaxBad if UserIsVa then UsFileT = FileText(UserFileDir..UserName) CorPassword, Privileges, LOBlockTimes, LOBadPwT, LOMaxBlock, LOMaxBad = UsFileT[1], UsFileT[2], UsFileT[3], UsFileT[4], UsFileT[5], UsFileT[6] print(CorPassword, Privileges, LOBlockTimes, LOBadPwT, LOMaxBlock, LOMaxBad) if Password == CorPassword then CurUser = UserName UserPiv = Privileges SignInTable = UsFileT NUserFile(UserName, Password, Privileges, 0, 0, LOMaxBlock, LOMaxBad, false) print("Signed In as "..UserName) return true else print("Incorrect Password for "..UserName..", Times: ".. LOBadPwT.."/"..LOMaxBad..", Blocked: "..LOBlockTimes.."/"..LOMaxBlock..".") if LOBadPwT >= LOMaxBad then LOBadPwT = 0 LOBlockTimes = LOBlockTimes + 1 local BlockedTime = LOBlockTimes * LOMaxBad print("You was blocked for "..BlockedTime.." seconds. Please wait.") Wait(BlockedTime) end end else local NewPri if #FileText(UserFileDir) > 0 then NewPri="Basic" else NewPri="Advanced" end print(NUserFile(UserName, Password, NewPri, 0, 0, 5, 5, true )) CurUser = UserName UserPiv = NewPri print("User Created") end end

-- Other --

-- New in Preview R2.04.2 -- Returns a string based on the Value function NumWord(Value, MaxValue) MaxValue = MaxValue or 10 local NumTable = { [0]="Zero", [1]="One", [2]="Two", [3]="Three", [4]="Four", [5]="Five", [6]="Six", [7]="Seven", [8]="Eight", [9]="Nine", [10]="Ten" }  if MaxValue>=Value then return NumTable[Value] end return tostring(Value) end

-- Shutdown local function Shutdown Running=nil end

-- OSEnv function OSEnv local running=true while running do     print("OSEnv>") local FAnswer = io.read if FAnswer == "" then running = false end print(os.getenv(FAnswer)) end end

-- Prints table contents. function PrintTable(t) for k,v in pairs(t) do     print(k,v) end end local PrintTable = PrintTable

-- New in Preview R2.04.2 -- Returns the Longest string in t. function LongestSIT(t) local MaxLength = 0 for k,v in pairs(t) do     if #tostring(v) > MaxLength then MaxLength = #v end end return MaxLength end

-- New in Preview R2.04.2 function PrintFiles local FileTableA = ReturnFiles print print(NumWord(#FileTableA).." Files:") local MaxLength = LongestSIT(FileTableA) for key,value in pairs(FileTableA) do     local Spaces = "" for i = 1, (MaxLength+3) - #tostring(value) do        Spaces=Spaces.." " end local ok, SizeBytes=pcall(FileSize, tostring(value)) if not ok then local NewTable = RemoveFileFromList(value) SaveTabToFi(RootPath..DirFile,NewTable) else print(key,value..Spaces.."Size: "..BytesPlus(SizeBytes)) end end end

-- Print the actions usable. local function PrintActions(PrintWhat) PrintTable(TAction) print((#TAction+1),"Clear") print((#TAction+2),"Exit") print((#TAction+3),"Shutdown") PrintFiles end

-- -- File Functions -- --

function SaveTabToFi(FileName,Table) local FileHandle=io.open(FileName,"w") FileHandle:setvbuf("full") for key,value in ipairs(Table) do     FileHandle:write(value) end FileHandle:flush FileHandle:close end

-- DirFile. (Actualy it is simply reading the lines of DirFile.)

-- Returns a table containing the text of FileName. function FileText(FileName) local FileTextT = { }  local FileHandle = io.open(RootPath..FileName, "r") if FileHandle == nil then return FileTextT end local LineText="" local Lines=0 while(LineText~=nil)do Lines = Lines + 1 LineText = FileHandle:read FileTextT[Lines] = LineText end FileHandle:close return FileTextT end local FileText = FileText

function ReturnFiles return FileText(DirFile) end local ReturnFiles = ReturnFiles

-- New in Preview R2.04 -- Hibernate function Hibernate(Argument) local FileHandle = io.open(RootPath..TerrainHiberFile, "w") print("Hibernate File Started") FileHandle:write("Saved","\n") print("Hibernate File: ".."10% (FileRunning)"..FileRunning) FileHandle:write(FileRunning,"\n") print("Hibernate File: ".."20% (Ready to save App Data)") if type(Argument) == "string" then FileHandle:write(Argument,"\n") print("Hibernate File: ".."100% (Ready to Hibernate)") end print("Hibernating...") local FileRunning = FileRunning if exit~= nil then exit end Shutdown FileHandle:close end

-- New in Preview R2.04 -- Resume from Hibernation function ReturnFromHibernate HibernateTable = FileText(TerrainHiberFile) local HibernateTable=HibernateTable if #HibernateTable > 1 then print("Resuming From Hibernation") PrintTable(HibernateTable) ResumeFromHibernation = true if HibernateTable[2] == "Lua" then Lua elseif HibernateTable[2] == "FileEdit" then FileEdit(HibernateTable[3]) else RunFile(HibernateTable[2], "") end end end

-- Find the length of a file --  filename: file name -- returns --  len: length of file --  asserts on error function FileSize(filename) local fh=assert(io.open(filename, "rb")) local len=assert(fh:seek("end")) fh:close return len end local FileSize=FileSize

-- New in Preview R2.04.2 -- Returns a better Multiple of Value: function BytesPlus(Value, Short) local ByteMulti=ByteMulti local DV1024=1/1024 local RValue1=Value local RValue2=1 for i=1, #ByteMulti do     if (RValue1*DV1024)>=1 then RValue1=(RValue1*DV1024) RValue2=RValue2+1 else break end end local Pl = "" if RValue1~= 1 then Pl = "s" end return (RValue1.." "..ByteMulti[RValue2]..Pl) end local BytesPlus=BytesPlus

-- Prints the FileSize. function PrintFileSize(FileName) if FileExists(FileName) then print(FileName.." Size: "..FileSize(RootPath..FileName).." Bytes") end end

-- Returns if the file exists. function FileExists(FileName) local FileList = ReturnFiles for i=1, #FileList do     ReadFN = FileList[i] if ReadFN == FileName then return true end end return false end local FileExists = FileExists

-- Returns if the user exists. function UserExists(UserName) local UserList = FileText(UserFileDir) for i=1, #UserList do     ReadFN = UserList[i] if ReadFN == UserName then return true end end return false end

-- Used to run the file. function RunFile(FileName, ParT) AddFile(FileName) if FileExists(FileName) then local TempFunction, Errors = loadfile(RootPath..FileName) if TempFunction==nil then print("Compile Error: ".. Errors) return -1, Errors end FileRunning = FileName Success, Errors = pcall(TempFunction, ParT) FileRunning = "TagSS" if(Success==false) then return -2, Errors end return 1, "Ok" end return -3, "File Not Found" end local RunFile = RunFile FileRun = RunFile

-- Add previously unknown file to FileList. function AddFile(FileName) local Error, FileSizeValue = pcall(FileSize,FileName) if Error == true then local Exists = FileExists(FileName) if Exists == false then local DirHandle = io.open(RootPath..DirFile, "a") DirHandle:write(FileName,"\n") DirHandle:close end end end local AddFile = AddFile

-- Used to create a file FileName, which is returned by this function. function NewFile(FileName) if FileName == nil then print("Usage: FileNew FileName") return end local DirHandle = io.open(RootPath..DirFile, "a") DirHandle:write(FileName,"\n") DirHandle:close local File = io.open(RootPath..FileName, "w") return File end local NewFile = NewFile FileNew = NewFile

-- Used to edit Lua Scripts. function FileEdit(FileName) FileRunning = "FileEdit" local TextTable = FileText(FileName) local running = true pcall(PrintFileSize, FileName) local FActionT = { "Override", "View", "End", "Hibernate" }  while running do      print("Editing "..FileName..", "..(#TextTable).." Lines") local FAnswer = io.read if FAnswer == "" then PrintTable(FActionT) elseif FAnswer == FActionT[1] then print("Line To Edit:") local LineToEdit = tonumber(io.read) print("Line Text: "..tostring(TextTable[LineToEdit])) print("Text to replace with:") local ReplaceText = io.read TextTable[LineToEdit] = ReplaceText elseif FAnswer == FActionT[2] then print("Line To View:") local LineToEdit = tonumber(io.read) print("Line Text: "..tostring(TextTable[LineToEdit])) elseif FAnswer == "End" then print("Now Saving..") running = false elseif FAnswer == "Hibernate" then HiberTag = 1 running = false end end local FileHandle = io.open(RootPath..FileName, "w") for i=1, #TextTable do     FileHandle:write(TextTable[i].."\n") end FileHandle:close PrintFileSize(FileName) if HiberTag == 1 then Hibernate(FileName) end end local FileEdit=FileEdit FilePrompt=FileEdit

-- Clears the DirFile. Returns true. local function ClearDirFile local DirHandle = io.open(RootPath..DirFile, "w") DirHandle:close return true end

-- Used by RemoveFile to remove FileName from DirFile. -- Returns the updated FileList (NFileList). function RemoveFileFromList(FileName) local FileList = ReturnFiles local NFileList = { }  local ReadFN = "" local ToNFiLi = 0 local IsRemoved = 0 for i=1, #FileList do     ReadFN = FileList[i] if ReadFN==FileName then IsRemoved = 1 elseif IsRemoved==1 then ToNFiLi = (i-1) NFileList[ToNFiLi] = FileList[i] else NFileList[i] = FileList[i] end end return NFileList end local RemoveFileFromList = RemoveFileFromList

-- Removes the file FileName, returning true. local function RemoveFile(FileName) local NFLi = RemoveFileFromList(FileName) local DirHandle = io.open(RootPath..DirFile, "w") for i=1, #NFLi do     DirHandle:write(NFLi[i],"\n") end DirHandle:close local File = os.remove(RootPath..FileName) return true end

-- This function is Removed. function Test print("Removed") end

-- New in Pre 2.03.1 -- Lua command prompt function Lua FileRunning = "Lua" local run = true function exit run = false end while run==true do     print("LuaPrompt>") local FunctionCall,Error = load( tostring(io.read) ) if FunctionCall ~= nil then local error, Result = pcall(FunctionCall) if error == false then print(error, Result) end else print(Error) end if TagSS.ShutdownTag then return end end end

-- Command Prompt function CommandPrompt print("Command Prompt Started") -- Main loop while Running do     print(path..">") Answer = io.read or "" local Par = 0 AnsT = { }     for word in string.gmatch(Answer, "%a+") do         Par = Par + 1 AnsT[Par] = word end if AnsT[1]==TAction[1] then Test elseif AnsT[1]==TAction[2] then FileHandle=NewFile(AnsT[2]) FileHandle:close FileEdit(AnsT[2]) elseif AnsT[1]==TAction[3] then RemoveFile(AnsT[2]) elseif AnsT[1]==TAction[4] then FilePrompt(AnsT[2]) elseif AnsT[1]==TAction[5] then OSEnv elseif AnsT[1]==TAction[6] then local error, result = pcall(Lua) print(result) elseif AnsT[1]==TAction[7] then SignIn(AnsT[2],AnsT[3]) elseif AnsT[1]==TAction[8] then PrintFileSize(AnsT[2]) elseif AnsT[1]==TAction[9] then Hibernate elseif AnsT[1]==TAction[10] then LowMemory elseif(AnsT[1]=="Clear")then clear elseif(AnsT[1]=="Exit")then Running = nil elseif(AnsT[1]=="Shutdown")then TagSS.ShutdownTag = true Running = nil elseif(AnsT[1]=="Actions") then PrintActions elseif Answer=="" then PrintActions else ErrorType = RunFile(AnsT[1], AnsT) if(ErrorType== -3)then print("No file or action named "..(AnsT[1]).." exists") end end end end

- -- End of Functions -- -

-- Action Table

TAction = {  "Help","FileNew","FileRemove","FileEdit","OSEnv","Lua","SignIn","GetFileSize","Hibernate","LowMemory" }

local TAction = TAction clear

print("TagSS "..(TagSSVersion))

-- Resume ReturnFromHibernate

-- Load a Custom Startup (New in Preview R2.03) RunFile("Startup", nil)

local Error, FailCode = pcall(CommandPrompt) if not Error then print("TagSS has Crashed.") print(tostring(FailCode)) else local FileHandle = io.open(RootPath..TerrainHiberFile, "w") FileHandle:close end

print("Press Enter to continue...") io.read

--