ihaveahax ba0272ff5d
Lua support (#878)
* Test implementation of lua

* Trust that lua knows what its doing with this

Silence warnings

* actually update top screen when Thingy is called, disable unnecessary ShowPrompt calls

* readme

* change init for a simple test, print error on top screen too

* Readme

* change init for a simple test, print error on top screen too

* enable more lua libs, edit init.lua with string examples

* one more readme edit before bed

* Readme

* change init for a simple test, print error on top screen too

* enable more lua libs, edit init.lua with string examples

* make lua a proper file type, add test UI library with two functions, remove luacmd command

* remove old attempts at editing lauxlib and liolib

* README

* README

* FS lib, new UI stuff

* consistency with "type* ptr" maybe

* add custom package searcher, reset package.path

* new functions for UI including basic print output buffer, add "Lua scripts..." option to home/power menu

* build vram0.tar including subdirs of data

* move default path to GM9LUA_DEFAULT_PATH

* FS_FileGetData

* testfgd, add GM9VERSION global, update README, fix indentation

* FS_FileGetData will return a nil instead if it fails

* it's actually luaL_pushfail

* os

* use luaL_tolstring instead of lua_tostring

* fix test/remove debugging showprompt

* os.clock float attempt

* fix print for real

* fix swapped offset and size for FileGetData

* finish OS stuff

* fix os.clock

* shorten table in/out

* remove .vscode dir

* enum test

* support building without lua

* NO_LUA hides menu options (except when you directly select a lua file)

* update UI lib to better match the ideas on #1

* dockermake

* add DrawPNG function

* whoops its ShowPNG

* minor fixes, add DrawPNG

* fix AskPrompt, add all showprompts mentioned in #1

* add newly added functions to readme

* try to keep separate code and data

* update lua to 5.4.7

* remove test libraries now that i want to attempt to implement in a real api

* add nix flake for building

* add various lua functions, some of them taken from the old attempt but with new names

* add dev shell to flake.nix

* remove test lua scripts in root

* add test lua scripts in data/luascripts

* add a whole bunch more lua functions and stuff

* add more test lua scripts

* add more functions, add preload script, add test io compatibility module

* add more functions and test scripts

* more functions and stuff

* more functions and stuff

* more functions and stuff, plus a wip ctrcheck reimpl

* yet more functions and stuff

* even more functions and stuff

* command comparison table.ods

* update command comparison table.ods

* update command comparison table.ods

* Add files via upload

* update command comparison table.ods

* update ui.show_text to use DrawStringCenter, update ctrcheck rewrite

* Split up the ARM9 code (.text, .vectors) and data (.rodata, .data, .bss) sections into their own ELFs.

This allows us to use more ARM9 WRAM while leaving the 128k BootROM mirror intact.

* use the makefile definition

* add title module, move around some functions, update command comparison table

* add readme for lua

* remove liolib.c and loslib.c

* more functions and things, use CheckWritePermissionsLuaError in place of more manual checks, update command comparison table

* add missing constant

* set CURRDIR to nil instead of "(null)" if not found

* remove gm9enum (unused since the restart)

* add ui.check_key

* split fs module to lua overlay and _fs internal module, and add a check for fs.write_file in lua

* add fs.ask_select_file and fs.ask_select_dir

* add fs.key_dump, replace overwrite_all and append_all with overwrite and append

* add fs.cart_dump and sys.emu_base

* add ctrtool, update flake.lock

* add io append mode

* make sure io.open with write mode starts with an empty file, add os.remove and os.rename aliases

* properly implement os.remove compatibility

* add fs.verify_with_sha_file, fix PathIsDirectory by using stat instead of opendir

* add util.running_as_module (untested)

* move scripts over to https://github.com/ihaveamac/GM9-lua-script-experiments

* remove ods and dockermake.sh

* remove data/scripts

* add lua autorun (untested)

* fix syntax error

* add sys.check_embedded_backup

* remove accidental symlink

* add sys.check_raw_rtc

* fix ui.show_file_text_viewer not freeing memory or reporting an error if OOM happens

* add todo notes for ui

* work-in-progress lua doc

* formatting fix

* up heading level for all sections

* Revert "up heading level for all sections"

This reverts commit 6ef14b619536b4253e341ba40b4dea728358979d.

* separators

* fix name and error for fs.move

* do explicit permission checks in fs.move

* fix error string for fs.copy

* fix function name for fs.dir_info

* fix error string for fs.find and fs.find_not

* fix function name for fs.img_umount

* partial fs doc

* finished fs doc, string fixes for fs module

* document fs.cart_dump encrypted opt, remove stat from fs.verify_with_sha_file

* title doc

* sys doc, error string updates

* util doc

* add json.lua to lua-doc

* add fs.find_all

* add 3dstool to flake

* make fs.find_all recursive actually recursive, document fs.find_all

* add "for" to comparison table

* change fs.find to return nil if no path was found, instead of raising an error

* change ui.echo to automatically word wrap (untested)

* Revert "change ui.echo to automatically word wrap (untested)"

This reverts commit 2524e7707708e9818162c31f9f004b6301a3061b.

* switch devkitNix to upstream

* flake.lock: Update

Flake lock file updates:

• Updated input 'devkitNix':
    'github:ihaveamac/devkitNix/883d173b94e3da8dc4cc0860cdda8c36b738817c' (2024-12-05)
  → 'github:bandithedoge/devkitNix/95fd44f4ac7cecf24edf22daa899a516df73c6b7' (2025-01-11)
• Updated input 'devkitNix/nixpkgs':
    'github:NixOS/nixpkgs/566e53c2ad750c84f6d31f9ccb9d00f823165550' (2024-12-03)
  → 'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
• Updated input 'hax-nur':
    'github:ihaveamac/nur-packages/c570b3830f7dd4d655afb109300529c896cd8855' (2024-12-05)
  → 'github:ihaveamac/nur-packages/cd49afba206c2eb10a349d92470fdf2cc942ae23' (2025-01-11)
• Updated input 'hax-nur/nixpkgs':
    'github:NixOS/nixpkgs/2c15aa59df0017ca140d9ba302412298ab4bf22a' (2024-12-02)
  → 'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/566e53c2ad750c84f6d31f9ccb9d00f823165550' (2024-12-03)
  → 'github:NixOS/nixpkgs/32af3611f6f05655ca166a0b1f47b57c762b5192' (2025-01-09)

* flake.lock: Update

Flake lock file updates:

• Updated input 'devkitNix':
    'github:bandithedoge/devkitNix/95fd44f4ac7cecf24edf22daa899a516df73c6b7' (2025-01-11)
  → 'github:bandithedoge/devkitNix/a344b0200a044f2d2ff99685f13ff7c53106428e' (2025-02-06)
• Updated input 'devkitNix/nixpkgs':
    'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
  → 'github:NixOS/nixpkgs/5b2753b0356d1c951d7a3ef1d086ba5a71fff43c' (2025-02-05)
• Updated input 'hax-nur':
    'github:ihaveamac/nur-packages/cd49afba206c2eb10a349d92470fdf2cc942ae23' (2025-01-11)
  → 'github:ihaveamac/nur-packages/2ce890cab4e948109ad1ad82ba18e69240a0d352' (2025-02-06)
• Updated input 'hax-nur/nixpkgs':
    'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
  → 'github:NixOS/nixpkgs/8532db2a88ba56de9188af72134d93e39fd825f3' (2025-02-02)
• Added input 'hax-nur/treefmt-nix':
    'github:numtide/treefmt-nix/bebf27d00f7d10ba75332a0541ac43676985dea3' (2025-01-28)
• Added input 'hax-nur/treefmt-nix/nixpkgs':
    follows 'hax-nur/nixpkgs'
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/32af3611f6f05655ca166a0b1f47b57c762b5192' (2025-01-09)
  → 'github:NixOS/nixpkgs/5b2753b0356d1c951d7a3ef1d086ba5a71fff43c' (2025-02-05)

* make devkitNix and hax-nur inputs follow nixpkgs

* Also dump section headers on .dis file

* prepare for upstream merge

* Restore original README.
* Remove flake, was only used for my own testing

* fix accidental removal of LIBS

* copy lua-doc.md into release archive

* README: update to mention Lua in place of GM9Script, add credits

* lua-doc: fix typo

* add sample HelloScript

* lua-doc: remove wip notice, since all gm9script features are replicated

* remove accidental inclusion of language.inl

* Fix mixture of tabs and spaces

* remove accidental nix leftover

* re-add @ for add2tar command

---------

Co-authored-by: luigoalma <luigoalma@hotmail.com>
Co-authored-by: Gruetzig <florianavilov@gmail.com>
Co-authored-by: Florian <88926852+Gruetzig@users.noreply.github.com>
Co-authored-by: Wolfvak <soherrera1@hotmail.com>
2025-03-21 08:25:04 +01:00

687 lines
22 KiB
C

#ifndef NO_LUA
#include "gm9os.h"
#include "timer.h"
#include "rtc.h"
#include "ui.h"
u64 osclock;
static inline bool isLeapYear(u32 year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
size_t getWeekday(bool abbreviated, char* out, u8 weekday) {
if (abbreviated) {
switch(weekday) {
case 1:
strcat(out, "Mon");
break;
case 2:
strcat(out, "Tue");
break;
case 3:
strcat(out, "Wed");
break;
case 4:
strcat(out, "Thu");
break;
case 5:
strcpy(out, "Fri");
break;
case 6:
strcat(out, "Sat");
break;
case 0:
case 7:
strcat(out, "Sun");
break;
default:
strcat(out, "");
return 0;
}
return 3;
}
switch(weekday) {
case 1:
strcat(out, "Monday");
return 6;
case 2:
strcat(out, "Tuesday");
return 7;
case 3:
strcat(out, "Wednesday");
return 9;
case 4:
strcat(out, "Thursday");
return 8;
case 5:
strcpy(out, "Friday");
return 6;
case 6:
strcat(out, "Saturday");
return 8;
case 7:
case 0:
strcat(out, "Sunday");
return 6;
default:
strcat(out, "");
return 0;
}
return 0;
}
size_t getMonthName(bool abbreviated, char* out, u8 month) {
if (abbreviated) {
switch(month) {
case 1:
strcat(out, "Jan");
break;
case 2:
strcat(out, "Feb");
break;
case 3:
strcat(out, "Mar");
break;
case 4:
strcat(out, "Apr");
break;
case 5:
strcat(out, "May");
break;
case 6:
strcat(out, "Jun");
break;
case 7:
strcat(out, "Jul");
break;
case 8:
strcat(out, "Aug");
break;
case 9:
strcat(out, "Sep");
break;
case 10:
strcat(out, "Oct");
break;
case 11:
strcat(out, "Nov");
break;
case 12:
strcat(out, "Dec");
break;
default:
strcat(out, "");
return 0;
}
return 3;
}
switch(month) {
case 1:
strcat(out, "January");
return 7;
case 2:
strcat(out, "February");
return 8;
case 3:
strcat(out, "March");
return 5;
case 4:
strcat(out, "April");
return 5;
case 5:
strcat(out, "May");
return 3;
case 6:
strcat(out, "Juny");
return 4;
case 7:
strcat(out, "July");
return 4;
case 8:
strcat(out, "August");
return 6;
case 9:
strcat(out, "September");
return 9;
case 10:
strcat(out, "October");
return 7;
case 11:
strcat(out, "November");
return 8;
case 12:
strcat(out, "December");
return 8;
default:
strcat(out, "");
return 0;
}
return 0;
}
u16 getDaysMonths(u32 months, u8 years) {
u8 daysInMonth[12] = {31, isLeapYear(2000 + years) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //is this ?: bad practice?
u16 ret;
for (u32 month = 0; month < months - 1; month++) {
ret += daysInMonth[month];
}
return ret;
}
u64 calcUnixTime(u8 years, u8 months, u8 days, u8 hours, u8 minutes, u8 seconds) {
u8 daysInMonth[12] = {31, isLeapYear(2000 + years) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //is this ?: bad practice?
u32 curdays;
u64 ret = 0;
ret += seconds;
ret += minutes * 60;
ret += hours * 60 * 60;
ret += (days - 1) * 24 * 60 * 60;
for (u16 year = 0; year < years + 30; year++) { //+30 because unix time starting in 1970 but rtc starts in 2000
if (isLeapYear(2000 + year)) {
curdays = 366;
} else {
curdays = 365;
}
ret += curdays * 24 * 60 * 60;
}
for (u16 month = 0; month < months - 1; month++) {
ret += daysInMonth[month] * 24 * 60 * 60;
}
return ret;
}
u64 getUnixTimeFromRtc(DsTime *dstime) {
u8
seconds = DSTIMEGET(dstime, bcd_s),
minutes = DSTIMEGET(dstime, bcd_m),
hours = DSTIMEGET(dstime, bcd_h),
days = DSTIMEGET(dstime, bcd_D),
months = DSTIMEGET(dstime, bcd_M),
years = DSTIMEGET(dstime, bcd_Y);
return calcUnixTime(years, months, days, hours, minutes, seconds);
}
u64 timer_usec( u64 start_time ) {
return timer_ticks( start_time ) / (TICKS_PER_SEC/1000000);
}
void weekdayfix(DsTime *dstime) {
int days = getUnixTimeFromRtc(dstime) / 86400; //days since thursday 1 1 1970
u8 weekday = (days + 5) % 7;
dstime->weekday = NUM2BCD(weekday);
}
void unixtodstime(u64 unixtime, DsTime *dstime) {
u32 seconds, minutes, hours, days, year, month;
seconds = unixtime;
minutes = seconds / 60;
seconds %= 60;
hours = minutes / 60;
minutes %= 60;
days = hours / 24;
hours %= 24;
year = 1970;
while(true)
{
bool leapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
u16 daysInYear = leapYear ? 366 : 365;
if(days >= daysInYear)
{
days -= daysInYear;
++year;
}
else
{
static const u8 daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(month = 0; month < 12; ++month)
{
u8 dim = daysInMonth[month];
if (month == 1 && leapYear)
++dim;
if (days >= dim)
days -= dim;
else
break;
}
break;
}
}
days++;
month++;
u8 bcd_year = year-2000;
dstime->bcd_Y = NUM2BCD(bcd_year);
dstime->bcd_M = NUM2BCD(month);
dstime->bcd_D = NUM2BCD(days);
dstime->bcd_h = NUM2BCD(hours);
dstime->bcd_m = NUM2BCD(minutes);
dstime->bcd_s = NUM2BCD(seconds);
dstime->weekday = 0;
}
bool my_strftime(char* _out, size_t _maxsize, const char* str, DsTime *dstime) { //my refers to github.com/Gruetzig
weekdayfix(dstime);
size_t strl = strlen(str);
size_t outpos = 0;
char out[_maxsize+10];
memset(out, 0, _maxsize+10);
u8 minute, hour, day, month, year, weekday, second, weeknumber;
u16 currentday, nextsunday, fyear;
char numbuf[3], numbuf2[5], numbuf3[9], numnum1[3], numnum2[3], numnum3[3];
if (!is_valid_dstime(dstime)) {
return false;
}
for (size_t i = 0;i<strl;i++) {
if (str[i] == '%') {
i++;
switch(str[i]) {
case 'a':
outpos += getWeekday(true, out, DSTIMEGET(dstime, weekday));
break;
case 'A':
outpos += getWeekday(false, out, DSTIMEGET(dstime, weekday));
break;
case 'b':
outpos += getMonthName(true, out, DSTIMEGET(dstime, bcd_M));
break;
case 'B':
outpos += getMonthName(false, out, DSTIMEGET(dstime, bcd_M));
break;
case 'c':
char buf[100];
my_strftime(buf, _maxsize-outpos-10, "%a %b %d %X %Y", dstime);
strcat(out, buf);
outpos += strlen(buf);
break;
case 'd':
day = DSTIMEGET(dstime, bcd_D);
if (day < 10) {
sprintf(numbuf, "0%d", day);
} else {
sprintf(numbuf, "%d", day);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'H':
hour = DSTIMEGET(dstime, bcd_h);
if (day < 10) {
sprintf(numbuf, "0%d", hour);
} else {
sprintf(numbuf, "%d", hour);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'I':
hour = DSTIMEGET(dstime, bcd_h);
if (hour > 12) {
hour = hour - 12;
}
if (!hour) {
hour = 12;
}
if (hour < 10) {
sprintf(numbuf, "0%d", hour);
} else {
sprintf(numbuf, "%d", hour);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'j':
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
if (currentday < 10) {
sprintf(numbuf, "00%d", currentday);
} else if (currentday < 100) {
sprintf(numbuf, "0%d", currentday);
} else {
sprintf(numbuf, "%d", currentday);
}
strcat(out, numbuf);
outpos += 3;
break;
case 'm':
month = DSTIMEGET(dstime, bcd_M);
if (month < 10) {
sprintf(numbuf, "0%d", month);
} else {
sprintf(numbuf, "%d", month);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'M':
minute = DSTIMEGET(dstime, bcd_m);
if (minute < 10) {
sprintf(numbuf, "0%d", minute);
} else {
sprintf(numbuf, "%d", minute);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'p':
hour = DSTIMEGET(dstime, bcd_h);
if (hour >= 12) {
strcat(out, "PM");
} else {
strcat(out, "AM");
}
outpos += 2;
break;
case 'S':
second = DSTIMEGET(dstime, bcd_m);
if (second < 10) {
sprintf(numbuf, "0%d", second);
} else {
sprintf(numbuf, "%d", second);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'U':
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
weekday = DSTIMEGET(dstime, weekday);
nextsunday = ((7-weekday)+currentday);
weeknumber = (nextsunday/7)+1;
if (weeknumber < 10) {
sprintf(numbuf, "0%d", weeknumber);
} else {
sprintf(numbuf, "%d", weeknumber);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'w':
weekday = DSTIMEGET(dstime, weekday);
if (weekday == 7) {
weekday = 0;
}
sprintf(out, "%d", weekday);
strcat(out, numbuf);
outpos++;
break;
case 'W':
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
weekday = DSTIMEGET(dstime, weekday);
nextsunday = ((8-weekday)+currentday);
weeknumber = (nextsunday/7);
if (weeknumber < 10) {
sprintf(numbuf, "0%d", weeknumber);
} else {
sprintf(numbuf, "%d", weeknumber);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'x':
month = DSTIMEGET(dstime, bcd_M);
day = DSTIMEGET(dstime, bcd_D);
year = DSTIMEGET(dstime, bcd_Y);
if (month < 10) {
sprintf(numnum1, "0%d", month);
} else {
sprintf(numnum1, "%d", month);
}
if (day < 10) {
sprintf(numnum2, "0%d", day);
} else {
sprintf(numnum2, "%d", day);
}
if (year < 10) {
sprintf(numnum3, "0%d", year);
} else {
sprintf(numnum3, "%d", year);
}
sprintf(numbuf3, "%s/%s/%s", numnum1, numnum2, numnum3);
strcat(out, numbuf3);
outpos += 8;
break;
case 'X':
hour = DSTIMEGET(dstime, bcd_h);
minute = DSTIMEGET(dstime, bcd_m);
second = DSTIMEGET(dstime, bcd_s);
if (hour < 10) {
sprintf(numnum1, "0%d", hour);
} else {
sprintf(numnum1, "%d", hour);
}
if (minute < 10) {
sprintf(numnum2, "0%d", minute);
} else {
sprintf(numnum2, "%d", minute);
}
if (second < 10) {
sprintf(numnum3, "0%d", second);
} else {
sprintf(numnum3, "%d", second);
}
sprintf(numbuf3, "%s:%s:%s", numnum1, numnum2, numnum3);
strcat(out, numbuf3);
outpos += 8;
break;
case 'y':
year = DSTIMEGET(dstime, bcd_Y);
if (year < 10) {
sprintf(numbuf, "0%d", year);
} else {
sprintf(numbuf, "%d", year);
}
strcat(out, numbuf);
outpos += 2;
break;
case 'Y':
fyear = DSTIMEGET(dstime, bcd_Y);
fyear += 2000;
sprintf(numbuf2, "%d", fyear);
strcat(out, numbuf2);
outpos += 4;
break;
case '%':
strcat(out, "%");
outpos++;
break;
default:
break; //not implemented
}
} else {
out[outpos] = str[i];
outpos++;
}
if (outpos > _maxsize) {
break;
}
}
strncpy(_out, out, _maxsize);
return true;
}
static int os_time(lua_State *L) {
int args = lua_gettop(L);
u64 unixtime;
switch(args) {
case 0:
DsTime dstime;
get_dstime(&dstime);
unixtime = getUnixTimeFromRtc(&dstime);
lua_pushinteger(L, unixtime);
return 1;
case 1:
lua_geti(L, 1, 1);
int year = lua_tointeger(L, -1);
lua_pop(L, 1);
if (year >= 2000) year -= 2000;
lua_geti(L, 1, 2);
int month = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_geti(L, 1, 3);
int day = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_geti(L, 1, 4);
int hour = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_geti(L, 1, 5);
int minute = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_geti(L, 1, 6);
int second = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_pushinteger(L, calcUnixTime(year, month, day, hour, minute, second));
return 1;
default:
return luaL_error(L, "not a valid amount of arguments");
}
}
static int os_date(lua_State *L) {
DsTime dstime;
get_dstime(&dstime);
char retbuf[100];
memset(retbuf, 0, 100);
int args = lua_gettop(L);
switch(args) {
case 0:
my_strftime(retbuf, 100, "%c", &dstime);
lua_pushstring(L, retbuf);
return 1;
case 1:
const char* str = lua_tostring(L, 1);
if ((strcmp(str, "*t") == 0 || strcmp(str, "!*t") == 0)) {
weekdayfix(&dstime);
//return table with date values
lua_newtable(L);
//year
lua_pushinteger(L, 2000+DSTIMEGET(&dstime, bcd_Y));
lua_setfield(L, -2, "year");
//month
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_M));
lua_setfield(L, -2, "month");
//wday (weekday)
lua_pushinteger(L, DSTIMEGET(&dstime, weekday));
lua_setfield(L, -2, "wday");
//yday (yearday)
lua_pushinteger(L, getDaysMonths(DSTIMEGET(&dstime, bcd_M), DSTIMEGET(&dstime, bcd_Y))+DSTIMEGET(&dstime, bcd_D));
lua_setfield(L, -2, "yday");
//day
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_D));
lua_setfield(L, -2, "day");
//hour
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_h));
lua_setfield(L, -2, "hour");
//minute
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_m));
lua_setfield(L, -2, "min");
//second
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_s));
lua_setfield(L, -2, "sec");
return 1;
} else {
my_strftime(retbuf, 100, lua_tostring(L, 1), &dstime);
lua_pushstring(L, retbuf);
return 1;
}
case 2:
if (lua_tointeger(L, 2) < 946684800) { //unix timestamp is 01.01.2000 00:00:00, so everything before is previous century and not supported
return luaL_error(L, "unix timestamp from before 2000 is not supported");
}
const char* str2 = lua_tostring(L, 1);
if ((strcmp(str2, "*t") == 0 || strcmp(str2, "!*t") == 0)) {
unixtodstime( lua_tointeger(L, 2) , &dstime);
weekdayfix(&dstime);
//return table with date values
lua_newtable(L);
//year
lua_pushinteger(L, 2000+DSTIMEGET(&dstime, bcd_Y));
lua_setfield(L, -2, "year");
//month
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_M));
lua_setfield(L, -2, "month");
//wday (weekday)
lua_pushinteger(L, DSTIMEGET(&dstime, weekday));
lua_setfield(L, -2, "wday");
//yday (yearday)
lua_pushinteger(L, getDaysMonths(DSTIMEGET(&dstime, bcd_M), DSTIMEGET(&dstime, bcd_Y))+DSTIMEGET(&dstime, bcd_D));
lua_setfield(L, -2, "yday");
//day
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_D));
lua_setfield(L, -2, "day");
//hour
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_h));
lua_setfield(L, -2, "hour");
//minute
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_m));
lua_setfield(L, -2, "min");
//second
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_s));
lua_setfield(L, -2, "sec");
return 1;
} else {
unixtodstime( lua_tointeger(L, 2) , &dstime);
my_strftime(retbuf, 100, str2, &dstime);
lua_pushstring(L, retbuf);
return 1;
}
default:
return luaL_error(L, "not a valid amount of arguments");
}
}
static int os_clock(lua_State *L) {
lua_pushnumber(L, timer_usec(osclock)/10000000.0);
return 1;
}
static int os_difftime(lua_State *L) {
u64 t2 = lua_tointeger(L, 1);
u64 t1 = lua_tointeger(L, 2);
lua_pushinteger(L, t2-t1);
return 1;
}
// os.remove and os.rename are done in data/luapackages/fs.lua
static const luaL_Reg os[] = {
{"clock", os_clock},
{"time", os_time},
{"date", os_date},
{"difftime", os_difftime},
{NULL, NULL}
};
int gm9lua_open_os(lua_State* L) {
luaL_newlib(L, os);
osclock = timer_start();
return 1;
}
#endif