Jump to content

NB Data System Framework


Atton

Recommended Posts

The NBDSF is a tool that I have been working on for a while now. I got really pissed off with SQL lite and wanted to see if I could make my own DB. After doing some reading with stuff related to C++ I got a bit of a better idea about how data works and all that. So after a few days of messing around I was able to make this tool.

  
-- The table that is used to store data into RAM. 
coreTable = {}; 
  
-- Used to split data 
function dataSplit (dataIn) 
local gate = true; 
local tag = false; 
local data = false; 
    for a,b in pairs (split(dataIn,'*')) do  
        if gate then 
            name = b; 
            coreTable[tostring(b)] = ""; 
            gate = false; 
        else 
            coreTable[name] = tostring(b); 
            gate = true; 
            name = false; 
            data = false; 
        end 
    end 
end; 
  
-- Read From File  
function readFromFile () 
local theFile = fileOpen("database.db"); 
local theData = fileRead(theFile,fileGetSize(theFile)); 
    tableD = split(theData,'~'); 
    for a,b in pairs (tableD) do 
        dataSplit(b); 
    end 
end; 
readFromFile (); 
  
-- Get's the data from the table. 
function getData (AC1) 
    if AC1 then 
        local InName = tostring(AC1); 
        local theData = coreTable[inName];  
        if theData then 
            return theData; 
        else 
            return false; 
        end 
    end 
end; 
  
-- Check’s if shit is original to prevent overwrites and database damage. 
function ifOriginal (name) 
    if name then 
        if coreTable[tostring(name)] then 
            return false; 
        else 
            return true; 
        end 
    end 
end; 
  
-- Checks data going to the table and protects the database from corruption. 
function checkData (username,data) 
    stat = tostring(string.find(tostring(username),"~")); 
  
    stat2 = tostring(string.find(tostring(username),"*")); 
  
    stat3 = tostring(string.find(tostring(data),"~")); 
  
    stat4 = tostring(string.find(tostring(data),"*")); 
    if username and data then 
        if stat == "nil" and stat2 == "nil" and stat3 == "nil" and stat4 == "nil" then 
            return true; 
        else 
            return false; 
        end 
    end 
end; 
  
-- Inject's data into table.  
function injectData (v1,v2) 
if v1 and v2 then 
    local name = tostring(v1); 
    local data = tostring(v2); 
    if name and data then 
                if checkData(name,data) then 
                    if ifOriginal(name) then 
                    coreTable[name] = ""; 
                    coreTable[name] = data; 
                    return true; 
                else 
                    return false; 
                end 
            end 
        end 
    end 
end; 
  
-- Writes to table. 
function writeToTable (v1,v2) 
        if v1 and v2 then 
            local name = tostring(v1); 
            local data = tostring(v2); 
            if name and data then 
                if getData(name) then 
                    if checkData (name,data) then 
                    coreTable[name] = ""; 
                    coreTable[name] = data; 
                    return true; 
                else 
                    return false; 
                end 
            end 
        end 
    end 
end; 
  
-- Used to write data to file in order to prevent information from being lost. 
function writeToFile () 
local theFile = fileOpen("database.db"); 
    for a,b in pairs (coreTable) do 
--      fileWrite(theFile,"\n".."~"..tostring(a).."*"..tostring(b)); This might be useful for making the db look clean. 
        fileWrite(theFile,"~"..tostring(a).."*"..tostring(b)); 
    end 
        fileClose(theFile); 
end; 
setTimer(writeToFile,120000,0); 
addCommandHandler("backup",writeToFile); 
  
-- Writes data to when the script closes. 
--[[ ```````````````````````````````````````]]-- 
addEventHandler ( "onResourceStop", root,  
    function ( resource ) 
        writeToFile (); 
    end 
); 
--[[ ```````````````````````````````````````]]-- 
  
function makeAccount (name,data) 
    if injectData(tostring(name),tostring(data)) then 
        return true; 
    else 
        return false; 
    end 
end; 
addEvent("NBDSF.makeAccount",true); 
addEventHandler("NBDSF.makeAccount",getRootElement(),makeAccount); 
  
function readAccount (name) 
    if name then 
        local data = getData(tostring(name)); 
        if data then 
            return tostring(name),tostring(data); 
        else 
            return false; 
        end 
    end 
end; 
addEvent("NBDSF.readAccount",true); 
addEventHandler("NBDSF.readAccount",getRootElement(),readAccount); 
  
function editAccount (v1,v2) 
    if v1 and v2 then 
        local name = tostring(v1); 
        local data = tostring(v2); 
        if name and data then 
            if writeToTable (name,data) then 
                return true; 
            else 
                return false; 
            end 
        end 
    end      
end; 
addEvent("NBDSF.editAccount",true); 
addEventHandler("NBDSF.editAccount",getRootElement(),editAccount); 
  
function dumpDataBase () 
for a,b in pairs (coreTable) do 
        return a,b; 
    end 
end; 
addEvent("NBDSF.dumpDataBase",true); 
addEventHandler("NBDSF.dumpDataBase",getRootElement(),dumpDataBase); 
  
  

It performs rather well and does not seem to run into any issues when I put it through testing. I was at one point able to load about 10,000 account into the system and it seems to work fine. But rather going on about testing I think I should explain how it works or at least try.

Tables are sort of like Arrays they can store data under a key sort of like this.

  
theTable 
Atton:LOLATUNAPASSWORD 
BUXBOI:PASSWORD1234 
theTableEnd 
  

You get the data by using the key so if I wanted my password I can use Atton as the key to find it on the DB. The only issue is that when you store data to tables it is stored in ram where if the script is stopped that information is destroyed. That is why non volatile memory was invented so you can just write it to your hard drive and read it when you need to restore it.

In the table the only data stored is the account name being used as the key and the password. You can defeat this using split and base64Encode for example.

Let say you wanted to store your username,password hash,cash,x,y,z you can do this.

username..","..hash..","..cash..","..x..","..y..","..z 

The "," is used to provide a break where the split function can work if you write one. I would suggest encoding this using base64 as it looks cleaner on the db.

~dbroot*dbroot~Atton*543798hghfkdhgfdjkhgjkfdhgjkfdhkjewqewqewq 

If you create your data structure you can write it to the DB rather easy with a few functions below.

editAccount Accepts two arguments (AccountName,Data) returns true or false

readAccount Accepts one argument (AccountName) returns the account name and data

makeAccount Accepts two arguments (AccountName,Data) returns true or false

dumpDataBase Returns the database stored in ram

They are all exports and should be rather easy to use, writing your data structure and making it load right is the hard part. I have not actually got around to making it work but you are welcome to make one and post it. But what do you guys think would you use this is it shit or what.

https://community.multitheftauto.com/in ... s&id=10683

Edited by Guest
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...