Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ General
➜ 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 page
| Posted by
| Grungeisdead
(2 posts) 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! | | Top |
|
| Posted by
| Donecce
(16 posts) 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. | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #2 on Fri 18 Sep 2015 08:44 PM (UTC) |
| Message
| |
| Posted by
| Grungeisdead
(2 posts) 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! | | 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.
17,991 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top