[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Save Contents of Text File as String

Save Contents of Text File as String

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Grungeisdead   (2 posts)  [Biography] bio
Date Thu 17 Sep 2015 10:45 PM (UTC)

Amended on Thu 17 Sep 2015 10:53 PM (UTC) by Grungeisdead

Message
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!
[Go to top] top

Posted by Donecce   (16 posts)  [Biography] bio
Date Reply #1 on Fri 18 Sep 2015 03:20 PM (UTC)
Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Fri 18 Sep 2015 08:44 PM (UTC)
Message
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

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Grungeisdead   (2 posts)  [Biography] bio
Date Reply #3 on Sat 19 Sep 2015 12:44 AM (UTC)
Message
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!
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


12,486 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]