View Single Post
  #6  
Old 12-11-2011, 08:17 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Crow View Post
Apparently, parseJSON() doesn't return true (anymore?). Not sure if it's only me, but I also tried restarting the server/NPC server. I tried feeding it some sample JSON stuff, and it also worked, but the function returned 0. Which makes gsync fail. Every time.

Edit: And for some strange reason, even when attempting to fix this for me, it doesn't work. The TStaticVar that send() returns is always null around line 51, even though it is not inside send(). What the heck?
You're right, very strange. Stefan must have changed something since release since now I'm getting the same errors.

Here's a fixed version (all it's doing is avoiding returning the entire JSON object, which fixes the issue—it also doesn't rely on parseJSON returning the correct value):

PHP Code:
enum LOG {
  
NONE,
  
ERROR,
  
INFO
};

function 
onInitialized() {
  
this.trigger("created");
}

function 
onCreated() {
  
this.join("func_http");
  
  
// options
  
this.server "http://gsync.graalcenter.org/gsync.php";
  
this.key "CHANGE_THIS";
  
this.verbosity LOG.INFO;
  
this.folders = {
    
"scripts",
    
"npcs",
    
"weapons",
    
"data"
  
};
  
  
// temp
  
this.sync();
}

public function 
sync() {
  
report(LOG.INFO"Starting sync");
  
  
// build list of files and their mod times
  
report(LOG.INFO"Building list of files and mod times");
  
temp.fileList "";
  
  for (
temp.folder this.folders) {
    
temp.files.loadFolder(temp.folder "/*"true);
    
    for (
temp.file files) {
      
temp.path temp.folder "/" temp.file;
      
temp.fileList @=
        
temp.path "\t" getFileModTime(temp.path) @ "\n";
    }
  }
  
  
report(LOG.INFO"Built list of files and mod times");
  
  
// send list of files & mod times to the server
  
report(LOG.INFO"Sending list of files and mod times to server");
  
  
temp.data = {
    {
"filelist"temp.fileList}
  };
  
  
temp.returnData this.send("filelist"temp.data"needed");
  
  if (
temp.returnData == null) {
    return 
report(LOG.ERROR"Didn't receive acceptable response from server");
  }
  
  
temp.neededFiles temp.returnData;
  
  
report(LOG.INFO"Server responded with needed files (" temp.neededFiles.size() @ ")");
  
  if (
temp.neededFiles.size() > 0) {
    
// send server all the files it wants
    
temp.data null;
    
temp.includedFiles "";
    
    for (
temp.neededFile temp.neededFiles) {
      
temp.contents.loadString(temp.neededFile);
      
temp.data.add({"file-" md5(temp.neededFile), temp.contents});
      
temp.data.add({"filem-" md5(temp.neededFile), getFileModTime(temp.neededFile)});
      
      
temp.includedFiles @= temp.neededFile "\n";
    }
    
    
temp.data.add({"files"temp.includedFiles});
    
    
report(LOG.INFO"Sending server requested files");
    
temp.returnData this.send("store"temp.data"status");
    
    if (
temp.returnData == null) {
      return 
report(LOG.ERROR"Didn't receive acceptable response from server");
    }
  }
  
  
report(LOG.INFO"Sync complete");
}

function 
send(temp.actiontemp.datatemp.needed) {
  
temp.data.add({"action"temp.action});
  
temp.data.add({"key"this.key});
  
  
temp.json = new TStaticVar("GSyncResponse" int(timevar2 100));
  
temp.response this.post(this.servertemp.data);
  
parseJSON(temp.jsontemp.response);
  
  if (
temp.json.status != null) {
    
report(LOG.INFO"Parsed JSON successfully in server's response to " temp.action);
    
    if (
temp.json.status == "ok") {
      
temp.response temp.json.(@ temp.needed);
      
temp.json.destroy();
      
      return 
temp.response;
    } else if (
temp.json.status == "error") {
      
report(LOG.ERROR"Received error from server for " temp.action ": " temp.json.error);
    } else {
      
report(LOG.ERROR"Received unexpected status code '" temp.json.status "' in response to " temp.action);
    }
  } else {
    
report(LOG.ERROR"Unable to parse JSON in server's response to " temp.action);
  }
  
  
temp.json.destroy();
}

function 
report(temp.logLeveltemp.msg) {
  if (
this.verbosity >= temp.logLevel) {
    echo(
"gsync: " temp.msg);
  }

I don't understand what could be causing this, either, though. Looks like it's probably one of GScript's weird glitches.
__________________
Reply With Quote