Save Contents of Text File as String

Posted by Grungeisdead on Thu 17 Sep 2015 10:45 PM — 4 posts, 18,977 views.

#0
I think I posted this in the wrong spot, oops. Can't seem to delete it or post it again in the proper one either. I suppose this'll have to do:

Hello,

I'm developing a combat counter for a MUD. It's written in javascript, and uses objects to save relevant data from mud output, store it, and make calculations based on it. I'm trying to develop an alternative method to get the data in the objects saving between sessions. Originally, I had been using a JSON function to stringify the object in question, and SetVariable to save the resulting string to the plugin's save state file. My bug testers discovered that eventually the save state file reaches a certain size, and mushclient can no longer load the data. So, I came up with an alternative idea, using mushclient's notepad functions:

function saveSets() {
  SetVariable("set",set);
  world.ReplaceNotepad("ccsave sets",JSON.stringify(sets));
  world.SaveNotepad("ccsave sets","c:/program files (x86)/mushclient/worlds/plugins/ccsavesets.txt",1);
  world.CloseNotepad("ccsave sets",0);
  world.ReplaceNotepad("ccsave options",JSON.stringify(options));
  world.SaveNotepad("ccsave options","c:/program files (x86)/mushclient/worlds/plugins/ccsaveoptions.txt",1);
  world.CloseNotepad("ccsave options",0);
  world.ReplaceNotepad("ccsave truedmg",JSON.stringify(truedmg));
  world.SaveNotepad("ccsave truedmg","c:/program files (x86)/mushclient/worlds/plugins/ccsavetruedmg.txt",1);
  world.CloseNotepad("ccsave truedmg",0);
};


This function stringifies the objects and saves them to text files, which works great. Unfortunately, I can't load the data again when I restart mushclient. I tried using world.Open, which did open mush notepads for the files, however when I tried to use world.GetNotepadText to send the contents of the files back to the script to be parsed back into objects, I discovered that these notepads aren't detectable like the notepads which are opened with functions like ReplaceNotepad. I've been looking around the internet for some way to open the text files, copy their contents, get those contents back into the script to be sent through JSON.parse, but I'm coming up with almost nothing that seems to accomplish this. I really need to figure this out, or my counter won't be able to save between sessions, unless I go back to the original method which b0rks after the save state reaches a certain size. Hoping you guys might be able to point me in the right direction, or dash my hopes entirely and tell me it just isn't possible.

Thanks!
Amended on Thu 17 Sep 2015 10:53 PM by Grungeisdead
#1
I don't know anything about javascript but..

If you were using lua, you could probably use the 'io' table of functions, io.open, io.read, io.write, io.close, etc.
Australia Forum Administrator #2
Once my variables reach a certain size I start using the SQLite3 functions. They are accessible from all the scripting languages, and are reasonably easy to use. The good thing is that you don't have to load massive amounts of data at startup (although you can) because you can just use a SQL query to find what you want.

For example, I used it to save mapper data for thousands of rooms.

See:

http://www.gammon.com.au/db

http://www.gammon.com.au/sql

http://www.gammon.com.au/scripts/doc.php?general=database
#3
Thank you for the suggestions guys, I was looking into SQL, but I was able to figure out a way to read/write through jscript (and i learned there's an important distinction between jscript and javascript!). Turns out jscript can interact with the file system through an activex object. This is the solution I came up with, in case anyone else is wondering if this can be done. It's working great:

function saveSets() {
  var fso, f;
  if (options.save.directory === undefined) {
    options.save.directory = world.GetPluginInfo(world.GetPluginID,20);
  }
  fso = new ActiveXObject("Scripting.FileSystemObject");
  f = fso.CreateTextFile(options.save.directory + "ccsave.txt",true);
  f.WriteLine(JSON.stringify(sets));
  f.WriteLine(JSON.stringify(options));
  f.WriteLine(JSON.stringify(truedmg));
  f.Close();
  ColourNote("#00B85C","#000000","Combat Counter data sets have been saved.");
};

function loadSets() {
  var fso,f,r,s,t;
  if (options.save.directory === undefined) {
    options.save.directory = world.GetPluginInfo(world.GetPluginID,20);
  }
  fso = new ActiveXObject("Scripting.FileSystemObject");
  if (fso.FileExists(options.save.directory + "ccsave.txt")) {
    f = fso.OpenTextFile(options.save.directory + "ccsave.txt",1);
    r = f.ReadLine();
    sets = JSON.parse(r);
    s = f.ReadLine();
    options = JSON.parse(s);
    t = f.ReadLine();
    truedmg = JSON.parse(t);
    f.Close();
  } else {
    sets = new Array();
  }
  set = options.sets.active;
};


Thanks again! Awesome to hear from Nick Gammon himself! Mushclient ftw!