Author Topic: Sub Addon Directory Reading  (Read 500 times)

0 Members and 1 Guest are viewing this topic.

Offline Shirakami

Sub Addon Directory Reading
« 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.

Code: [Select]
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.

« Last Edit: July 18, 2017, 02:16:28 PM by Shirakami »

Offline Phystam

Re: Sub Addon Directory Reading
« Reply #1 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!!

Offline Shirakami

Re: Sub Addon Directory Reading
« Reply #2 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

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4224
  • Total likes: 168
  • Helpful: 148
  • Languages: EN, DE, AT
Re: Sub Addon Directory Reading
« Reply #3 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?
Parsley, sage, rosemary, and maggikraut.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 4674
  • Total likes: 170
  • Helpful: 108
  • Languages: EN, NO
Re: Sub Addon Directory Reading
« Reply #4 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.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 8745
  • Total likes: 305
  • Helpful: 229
  • Languages: De,EN,JP
Re: Sub Addon Directory Reading
« Reply #5 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