The International Simutrans Forum

Development => Patches & Projects => Topic started by: Shirakami on July 18, 2017, 04:44:50 AM

Title: Sub Addon Directory Reading
Post by: Shirakami on July 18, 2017, 04:44:50 AM
Hello!

I wrote the code I wanted for many years.

Code to read sub directory in addons directory.

It will make it easier to organize Pak files.


simmain.cc line:1019

// loading all paks
dbg->important("Reading object data from %s...", env_t::objfilename.c_str());
obj_reader_t::load(env_t::objfilename.c_str(), translator::translate("Loading paks ...") );
if(  env_t::default_settings.get_with_private_paks()  ) {
  // try to read addons from private directory
  chdir( env_t::user_dir );
  if(!obj_reader_t::load(("addons/" + env_t::objfilename).c_str(), translator::translate("Loading addon paks ..."))) {
    fprintf(stderr, "reading addon object data failed (disabling).\n");
    env_t::default_settings.set_with_private_paks( false );
  }

+   // Search secondary directory
+   searchfolder_t sf;
+   string udn = env_t::user_dir;
+   sf.search(udn + "addons/" + (env_t::objfilename).c_str(), "", true);

+   FOR(searchfolder_t, const& i, sf) {
+     char oDirectoryName[1024];
+     strcpy(oDirectoryName, i);
+     strcat(oDirectoryName, "/");
+     obj_reader_t::load(oDirectoryName, translator::translate("Loading addon paks ..."));
+   }

  chdir( env_t::program_dir );
}


P.S.
I made a patch file.

Title: Re: Sub Addon Directory Reading
Post by: Phystam on July 21, 2017, 11:48:38 PM
Great! If you can, please write it as a recursive function!!! ;)
Then the management of our addons become much easier!!
Title: Re: Sub Addon Directory Reading
Post by: Shirakami on July 24, 2017, 12:43:26 AM
The code was improved.

* It is possible to Load not only the Addons directory but also the sub directory in the Pak directory.
* I created a binary file.
https://www.dropbox.com/s/x9hmr639wolx9xc/subdirectory.zip?dl=0 (https://www.dropbox.com/s/x9hmr639wolx9xc/subdirectory.zip?dl=0)
Title: Re: Sub Addon Directory Reading
Post by: Dwachs on July 24, 2017, 07:13:46 AM
Please check this old thread: http://forum.simutrans.com/index.php?topic=1917.0 , It was a similar patch, forgotten to be included, all attachments lost.

There was the request to exclude certain directories from automatic loading, which might be unwanted. Could you include something like this in your patch?
Title: Re: Sub Addon Directory Reading
Post by: Ters on July 24, 2017, 04:48:12 PM
This patch adds yet another unsafe string concatenation to simmain. (The other one is shortly after the comment "still nothing to be loaded => search for demo games".) There are a couple of safer examples, using either cbuffer_t or std::string. It is better to do like one of those.
Title: Re: Sub Addon Directory Reading
Post by: prissi on July 24, 2017, 05:06:19 PM
Since the user should never touch the pak directories, like old paks onlz needed to load old games etc. I would advice against including subdirectories in the regulars pak directories.
For addons, one can think of two buttons, addons and "all addons", the latter with subdirectories