Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for multi file projects #8

Open
atesin opened this issue Sep 4, 2024 · 12 comments
Open

add support for multi file projects #8

atesin opened this issue Sep 4, 2024 · 12 comments
Labels
enhancement New feature or request

Comments

@atesin
Copy link

atesin commented Sep 4, 2024

you read this discussion in tic80 issues page: nesbox/TIC-80#2668 ... i better keep analisys there and move technical questions here

i have some ideas on how to implement this, i hope they were not so hard, maybe the most practical woule be to let the package do all the work for us everytime, merging project files in a temp thus killing the need for require()'s also... something like:

  • cat mylib1.lua > temp.lua
  • cat mylib2.lua >> temp.lua
  • cat maincode.lua >> temp.lua
  • tic80.exe --cmd="load mycart & import temp.lua & save & run"
    • create cart if not exists> tic80.exe --cmd="new lua & import temp.lua & save mycart & run"
  • rm -f temp.lua

but you need some way to specify project cart name before: maybe right click on on project > set cart name, or menu project > properties > esit cart name, or whatever (i dont know how, i a not a plugin/package developer nor have experience with zbs)

@atesin
Copy link
Author

atesin commented Sep 4, 2024

could you please teach me how to submit pull requests (have to fork before?)?? ... i have some ideas, i want you to code them and show you, and if you like them merge them

my email is like my username, but in gmail

@oblerion oblerion added the enhancement New feature or request label Sep 5, 2024
@oblerion
Copy link
Owner

oblerion commented Sep 5, 2024

could you please teach me how to submit pull requests (have to fork before?)?? ... i have some ideas, i want you to code them and show you, and if you like them merge them

Yes, you need to make a fork (your copy of the project), edit it and make a pull request when you agree.
Your pull request is updated at the same time as your fork edit.
And don't worry, you need approval before merging.

@oblerion
Copy link
Owner

oblerion commented Sep 5, 2024

You can do cat lib.lua >> tmp_cart.lua in lua

function cat(lib, tmp_cart)

end

I will do parsing for use your function each time it see:

--import lib.lua

in game lua cart.

And use

--cart tmp_cart.lua

for set cart output name.

@atesin
Copy link
Author

atesin commented Sep 5, 2024

for this to work i think we need some info before:

  • path to tic80.exe (global, done)
  • current work dir (it works some misterious way, done)
  • list of current lua zbs project files (i don't know how to get it)
  • someway to set name of final cart file (per project, pending)
  • some way to operate with file contents to merge them... maybe lua i/o facilities: https://lua.org/manual/5.2/manual.html#6.8

also i suggest to change config variable names from xxx.tic80 to tic80.xxxx instead, going from general to particular, this way is more easier to group/find more future tic80 related variables

@atesin
Copy link
Author

atesin commented Sep 7, 2024

this can be done even with an ms-dos batch!

while working a tic80 project with zbs, in parallel, supposing we are in <tic80-fs-folder>\merge-test subfolder:

type > mergedlua.tmp
type *.lua >> mergedlua.tmp
if exist mycart.tic (
  F:\Games\tic80\tic80.exe mycart.tic --cmd="import code merge-test/mergedlua.tmp & save & run"
) else (
  F:\Games\tic80\tic80.exe --cmd="new lua & import code merge-test/mergedlua.tmp & save merge-test/mycart & run"
)
del mergedlua.tmp

i am using this and it works flawlesly... question is, how to convert this batch to a zbs lua package, to make it easy for everyone

we must know previously:

  • tic80.exe path
  • all paths inside tic80 console and --cmd argument are relative to tic80 filesystem folder, no matter the host system current work dir (i.e. chrooted) (see -fs arg and folder cmd) (not the case for embedded lua interpreter that inherits sysenv, see tic80 doesn't find my required lua files nesbox/TIC-80#2426 (comment) just as example because merging files makes require() unnecesary)
  • tic80 filesystem folder absolute path, to substract to current file paths for -cmd arguments
  • curent project absolute path, to get relative paths by substracting <tic80-fs-folder> above
  • some way to set a name for mycart.tic for each project (with many files opened, zbs just take the file name of the SELECTED TAB)
  • maybe some random name for mergedlua.tmp

limitations:

  • all files must/will be in the same folder (luas and tic) neverless they are opened or not (works with saved version)
  • del mergedlua.tmp waits execution until tic80 closes
  • maybe take care of global variables be defined before tic80 uppercase functions api (TIC(), BOOT(), etc.)

@oblerion
Copy link
Owner

oblerion commented Sep 8, 2024

this can be done even with an ms-dos batch!
i am using this and it works flawlesly... question is, how to convert this batch to a zbs lua package, to make it easy for everyone

Batch work only for window, you need this functions for translate in lua.

function string.cmp(str,str2) -- string is egal
  if str:len() ~= str2:len() then
    return false
  end
  for ic=1,#str do
    if str:byte(ic) ~= str2:byte(ic) then
      return false
    end
  end
  return true
end
function getos()
  local BinaryFormat = package.cpath:match("%p[\\|/]?%p(%a+)")
  if BinaryFormat == "dll" then
    return "Windows"
  elseif BinaryFormat == "so" then
    return "Linux"
  elseif BinaryFormat == "dylib" then
    return "MacOS"
  end
  BinaryFormat = nil
  return ""
end
function scandir(directory) -- get array of file name
  local i, t, popen = 0, {}, io.popen
  local pfile 
  if string.cmp(getos(),"Linux")==true or string.cmp(getos(),"MacOS")==true then 
    pfile = popen('ls -a "'..directory..'"')
  else
    pfile = popen('dir '..directory)
  end
  for filename in pfile:lines() do
    i = i + 1
    t[i] = filename
  end
  pfile:close()
  return t
end
function is_fileext(path,ext) -- check file extension
  local extsize = ext:len()-1
  local extstring = path:sub(path:len()-extsize,path:len())
  if string.cmp(extstring,ext)==true then
    return true
  end
  return false
end
function is_dir(path)
  local f = io.open(path, "r")
  local ok, err, code = f:read(1)
  f:close()
  return code == 21
end
function is_file(path)
  local f = io.open(path, "r")
  if f~=nil then
    f:close()
    return true
  end
  return false
end
function create_file(path)
  local fic = io.open(path,"w")
  fic:write("")
  fic:close()
end
function del_file(path)
  if not is_dir(path)==true then
    if string.cmp(getos(),"Linux")==true or string.cmp(getos(),"MacOS")==true then 
      popen('rm '..path)
    else
      popen('del '..path)
    end
  end
end
function cat_file(src,out)
  if is_file(src)==true and is_file(out)==true then
    popen('cat '..src..' >> '..out)
  end
end

an example

local d = scandir('.') -- array of file name
for i=1,#d do
  if is_fileext(d[i],'.lua')==true then
-- print only lua file
    print(d[i])
  end
end

note

  • popen(system_command)

calls the command and returns a string with the result

@oblerion
Copy link
Owner

oblerion commented Sep 8, 2024

all files must/will be in the same folder (luas and tic) neverless they are opened or not (works with saved version)

it need to define tic80.libpath in config file
#9

@oblerion
Copy link
Owner

oblerion commented Sep 8, 2024

also i suggest to change config variable names from xxx.tic80 to tic80.xxxx instead, going from general to particular, this way is more easier to group/find more future tic80 related variables

#9

@atesin
Copy link
Author

atesin commented Sep 17, 2024

i have a general idea on how this new feature could work, but i am boiling my head since many days in front of my editor trying to figure out how to do this (i suck at programming, augmented with an awful documentation xD )... maybe you know how to do it:

  • when select any "TIC_80_*" interpreter, a new submenu "set cart name" appear in "project" menu, when select other interpreter it goes out
  • when click that submenu a new "text prompt" popup window appear, asking for a valid cart name (you know, like javascript "prompt()" windows)
  • when execute "run project", it first checks if this project has a cart name set, if not a popup window alerts you, either an informative "alert()", or a "confirm()" popup if you want to set the cart name now, in which case the mentioned "set cart name" prompt() windows appears
  • for this plugin, a list of cart names by project could be stored in a ZBS table, using project directory path as index, as explained here = https://studio.zerobrane.com/doc-plugin#plugin-data , so the previous point is actually a runtime lookup between the current project dir and this table
  • a dirty but quick and easy way to launch TIC-80, avoiding all those path string processing lookup and parsing, could be just with -fs PROJECT_DIR option, anyway we will use zbs+plugin to develop TIC-80 carts instead playing them finished :)
  • instead of using some elaborated native lua solutions for merging lua files, we could just rely on simple system commands to merge *.lua files to another temporary file other type to avoid merging itself :) :
    • in everything else NOT Windows (i.e. unix like): echo -n > CART_NAME.lua.tmp ; cat *.lua >> CART_NAME.lua.tmp
  • finally run TIC-80 with one of these commands:
    • if CART_NAME file not exists: tic80.exe -fs PROJECT_DIR -cmd="new lua & import code CART_NAME.lua.tmp & save CART_NAME & run"
    • else if cart does exists: tic80.exe -fs PROJECT_DIR -cmd="load CART_NAME & import code CART_NAME.lua.tmp & save & run"

what do you think?.. do you think is possible?, do you have an idea on how to achieve these?... do you like them ? :)

@atesin
Copy link
Author

atesin commented Sep 18, 2024

some resources i could have found: official ZBS plugin documentation (from homepage) = https://studio.zerobrane.com/doc-plugin , there are some info like:

  • some menu api examples (modify menus, add, remove menu items, etc)
  • some interesting plugin events, particulary onInterpreterLoad|...Close when select/deselect some interpreter from menu project > interpreter for example
  • for current project dir i tried ide.config.path.projectdir, but i am sure there are many ways maybe better to get it
  • more resources i don't even imagine

@atesin
Copy link
Author

atesin commented Sep 27, 2024

some simplifications:

  • on compiling project without setting cart name, instead of a intermediate popup alert saying "cart name not set" just open the prompt popup window to set name directly
  • the additional menu to set cart name ("Set TIC-80 cart name...") could be added just once and permanently at package register time instead enabling and disabling each time we switch interpreter (although is useless with other interpreters)
  • this menu could function similarly to "menu > project > command line parameters", which also opens a js-prompt style popup
  • there is a documentation example on how to add items to zbs menu bars = https://studio.zerobrane.com/doc-plugin#example-adding-a-menu-item-and-a-toolbar-button-that-run-make (see where it says "This plugin demonstrates how to add a menu item")

i honestly tried to modify the plugin by myself, but i bumped with lack of documentation... for example there is an ID() function that i have no idea what it does, also this function seems to accepts constant-looking parameters that are specified nowhere... i searched for definitions in zbs code with dngrep and in zbs and wxwidgets documentation but i found nothing related, i am very frustrated with this :(

@atesin
Copy link
Author

atesin commented Oct 7, 2024

look at this ... maybe we can use some of the tools mentioned here, some are written in lua:

nesbox/TIC-80#2426 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants