public bool SetStats(string userID, string titleID, string base64Data) { try { var filename = ""; if (titleID == "iw4") { filename = "iw4.stat"; } else if (titleID == "iw5") { filename = "mpdata"; } var npid = (uint.Parse(userID) | 0x110000100000000); var fsFile = StorageUtils.GetFilename(filename, npid); var data = Convert.FromBase64String(base64Data); if (!File.Exists(fsFile)) { return(false); } string backupFile = null; //var result = RPCStorageWriteUserFileMessage.PrepareWriteFile(npid, "iw4.stat", data, out backupFile); var result = false; if (!result) { return(false); } // recalculate checksum var checksumBytes = new byte[data.Length - 4]; Array.Copy(data, 4, checksumBytes, 0, checksumBytes.Length); var checksum = Crc32.Compute(checksumBytes); Array.Copy(BitConverter.GetBytes(checksum), 0, data, 0, 4); try { File.WriteAllBytes(fsFile, data); } catch (Exception ex) { Log.Error(ex.ToString()); return(false); } } catch (Exception ex) { Log.Error(ex.ToString()); throw ex; } return(true); }
public override void Process(NPHandler client) { var fileName = Message.fileName; var npid = (long)Message.npid; var fsFile = StorageUtils.GetFilename(fileName, npid); if (!File.Exists(fsFile)) { fsFile = "data/priv/" + npid.ToString("x16") + "/" + fileName.Replace("\\", "/").Replace("/", ""); } _client = client; _fileName = fileName; _npid = npid; if (!client.Authenticated) { ReplyWithError(2); return; } if (client.NPID != (long)npid) { ReplyWithError(2); return; } Log.Info("Got a request for file " + fileName + " for user " + npid.ToString("x16")); if (File.Exists(fsFile)) { try { var stream = File.OpenRead(fsFile); _readBuffer = new byte[(int)stream.Length]; stream.BeginRead(_readBuffer, 0, _readBuffer.Length, ReadCompleted, stream); } catch (Exception ex) { Log.Error(ex.ToString()); ReplyWithError(3); } } else { ReplyWithError(1); } }
private void Handle(UpdateRequest request) { Log.Info("Handling profile update request for " + request.UserID.ToString("X16")); var filename = StorageUtils.GetFilename("iw4.stat", request.UserID); if (!File.Exists(filename)) { return; } _structuredData.IW5 = true; _structuredData.SetData(File.ReadAllBytes(filename)); var prestige = _structuredData.Get("prestige").Get <int>(); var experience = _structuredData.Get("experience").Get <int>(); var cardicon = _structuredData.Get("cardIcon").Get <string>(); var cardtitle = _structuredData.Get("cardTitle").Get <string>(); var shortUserID = (int)(request.UserID & 0xFFFFFFFF); var profileData = new IW4Profiles(); var isNew = true; var query = from profile in Database.IW4Profiles where profile.UserID == shortUserID select profile; if (query.Count() > 0) { profileData = query.First(); isNew = false; } profileData.UserID = shortUserID; profileData.Prestige = (sbyte)prestige; profileData.Experience = experience; profileData.CardIcon = cardicon; profileData.CardTitle = cardtitle; if (isNew) { Database.IW4Profiles.InsertOnSubmit(profileData); } Database.SubmitChanges(); }
public string GetStats(string userID, string titleID) { var filename = ""; if (titleID == "iw4") { filename = "iw4.stat"; } else if (titleID == "iw5") { filename = "mpdata"; } var npid = (uint.Parse(userID) | 0x110000100000000); var fsFile = StorageUtils.GetFilename(filename, npid); if (!File.Exists(fsFile)) { return(""); } return(Convert.ToBase64String(File.ReadAllBytes(fsFile))); }
public override void Process(NPHandler client) { var fileName = Message.fileName; var fileData = Message.fileData; var npid = (long)Message.npid; var fsFile = StorageUtils.GetFilename(fileName, npid); _client = client; _fileName = fileName; _npid = npid; if (!client.Authenticated) { ReplyWithError(1); return; } if (client.NPID != (long)npid) { ReplyWithError(1); return; } if (!Directory.Exists(Path.GetDirectoryName(fsFile))) { Directory.CreateDirectory(Path.GetDirectoryName(fsFile)); } // are we allowed to write this type of file? if (!_fileHooks.ContainsKey(fileName)) { ReplyWithError(1); return; } string backupFile = null; int result = _fileHooks[fileName](fileData, fsFile, out backupFile); if (result > 0) { ReplyWithError(result); return; } Log.Info(string.Format("Got a request for writing {0} bytes to file {1} for user {2}.", fileData.Length, fileName, npid.ToString("X16"))); try { var stream = File.Open(fsFile, FileMode.Create, FileAccess.Write); stream.BeginWrite(fileData, 0, fileData.Length, WriteCompleted, stream); if (backupFile != null) { var backupStream = File.Open(backupFile, FileMode.Create, FileAccess.Write); backupStream.BeginWrite(fileData, 0, fileData.Length, BackupWriteCompleted, backupStream); } } catch (Exception ex) { Log.Error(ex.ToString()); ReplyWithError(2); } }