Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aglitchman/defold-savetable
Encode/decode Lua tables into a string. It's a native extension for the Defold game engine.
https://github.com/aglitchman/defold-savetable
defold defold-game-engine defold-library defold-module lua
Last synced: 3 months ago
JSON representation
Encode/decode Lua tables into a string. It's a native extension for the Defold game engine.
- Host: GitHub
- URL: https://github.com/aglitchman/defold-savetable
- Owner: aglitchman
- License: cc0-1.0
- Created: 2021-09-03T10:42:01.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-06-07T22:55:42.000Z (over 2 years ago)
- Last Synced: 2024-11-02T14:33:26.758Z (3 months ago)
- Topics: defold, defold-game-engine, defold-library, defold-module, lua
- Language: C++
- Homepage:
- Size: 12.7 KB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# SaveTable
⚠️ Defold has implemented [`sys.serialize()`/`sys.deserialize()`](https://defold.com/ref/beta/sys/#sys.serialize:table) since version 1.2.188. Use the new functions instead of this extension. ⚠️
## Description
This asset allows you to serialize/deserialize Lua tables in Defold the same way as it does via `sys.save`/`sys.load`. The limits are the same:
> Internally, this function uses a workspace buffer sized output file sized 512kb. This size reflects the output file size which must not exceed this limit. Additionally, the total number of rows that any one table may contain is limited to 65536 (i.e. a 16 bit range). When tables are used to represent arrays, the values of keys are permitted to fall within a 32 bit range, supporting sparse arrays, however the limit on the total number of rows remains in effect.
## Installation
Use it in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your `game.project` file and in the dependencies field under project add:
https://github.com/aglitchman/defold-savetable/archive/main.zip
## Usage
```lua
local tbl = { example = true }-- Encode Lua table into a base64-encoded string
local str = savetable.encode_base64(savetable.serialize(tbl))
print(str)-- Decode Lua table from a string
local tbl2 = savetable.deserialize(savetable.decode_base64(str))
pprint(tbl2)
```### How to use it in [DefSave](https://github.com/subsoap/defsave) for HTML5 builds
`defsave/defsave.lua`, replace lines 124-131 with:
```lua
local loaded_file
if html5 then
-- sys.load can't be used for HTML5 apps running on iframe from a different origin (cross-origin iframe)
-- use `localStorage` instead because of this limitation on default IndexedDB storage used by Defold
local EMPTY_TABLE = savetable.encode_base64(savetable.serialize({}))
loaded_file = savetable.deserialize(savetable.decode_base64(html5.run([[(function(){try{return window.localStorage.getItem(']] .. path .. [[')||']] .. EMPTY_TABLE .. [['}catch(e){return']] .. EMPTY_TABLE .. [['}})()]])))
else
loaded_file = sys.load(path)
end
````defsave/defsave.lua`, replace lines 183-194 with:
```lua
local is_save_successful
if html5 then
-- sys.save can't be used for HTML5 apps running on iframe from a different origin (cross-origin iframe)
-- use `localStorage` instead because of this limitation on default IndexedDB storage used by Defold
local encoded_data = savetable.encode_base64(savetable.serialize(M.loaded[file].data))
html5.run([[try{window.localStorage.setItem(']] .. path .. [[', ']] .. encoded_data .. [[')}catch(e){}]])is_save_successful = true
else
is_save_successful = sys.save(path, M.loaded[file].data)
end
```## Credits
This project is licensed under the terms of the CC0 1.0 Universal license.