unit UnitXYZ;
interface
uses
Classes, SysUtils;
type
TStringReaderWriter =
class helper
for TStream
function ReadString:
string;
procedure WriteString(
const AValue:
string);
end;
TMyGameObject =
class
private
type
TExample =
class
private
var
FExampleString:
string;
FExampleInteger: LongInt;
procedure SetExampleInteger(
const Value: LongInt);
procedure SetExampleString(
const Value:
string);
public
procedure SaveToStream(
const ATarget: TStream);
procedure LoadFromStream(
const ASource: TStream);
property ExampleString:
string read FExampleString
write SetExampleString;
property ExampleInteger: LongInt
read FExampleInteger
write SetExampleInteger;
end;
var
FInterestingString:
string;
FExampleObject: TExample;
procedure SetInterestingString(
const Value:
string);
public
constructor Create;
destructor Destroy;
override;
procedure SaveToStream(
const ATarget: TStream);
procedure LoadFromStream(
const ASource: TStream);
procedure SaveToFile(
const AFilename:
String);
procedure LoadFromFile(
const AFilename:
String);
property InterestingString:
string read FInterestingString
write SetInterestingString;
property ExampleObject: TExample
read FExampleObject;
end;
implementation
{ TMyGameObject.TExample }
procedure TMyGameObject.TExample.LoadFromStream(
const ASource: TStream);
begin
FExampleString := ASource.ReadString;
ASource.ReadBuffer(FExampleInteger, SizeOf(FExampleInteger));
end;
procedure TMyGameObject.TExample.SaveToStream(
const ATarget: TStream);
begin
ATarget.WriteString(FExampleString);
ATarget.WriteBuffer(FExampleInteger, SizeOf(FExampleInteger));
end;
procedure TMyGameObject.TExample.SetExampleInteger(
const Value: LongInt);
begin
FExampleInteger := Value;
end;
procedure TMyGameObject.TExample.SetExampleString(
const Value:
string);
begin
FExampleString := Value;
end;
{ TStringReaderWriter }
function TStringReaderWriter.ReadString:
string;
var
ResultString: AnsiString;
StringSize: Integer;
begin
Result := '
';
ReadBuffer(StringSize, SizeOf(StringSize));
SetLength(ResultString, StringSize);
ReadBuffer(Pointer(ResultString)^, StringSize);
{$ifdef UNICODE}
Result := Utf8ToString(ResultString);
{$else}
Result := Utf8Decode(ResultString);
{$endif}
end;
procedure TStringReaderWriter.WriteString(
const AValue:
string);
var
StringSize: Integer;
StringToSave: AnsiString;
begin
StringToSave := Utf8Encode(AValue);
StringSize := Length(StringToSave);
WriteBuffer(StringSize, SizeOf(StringSize));
WriteBuffer(Pointer(StringToSave)^, StringSize);
end;
{ TMyGameObject }
constructor TMyGameObject.Create;
begin
FExampleObject := TExample.Create;
end;
destructor TMyGameObject.Destroy;
begin
FExampleObject.Free;
inherited;
end;
procedure TMyGameObject.LoadFromFile(
const AFilename:
String);
var
FileContents: TFileStream;
begin
FileContents := TFileStream.Create(AFilename, fmOpenRead);
try
LoadFromStream(FileContents);
finally
FileContents.Free;
end;
end;
procedure TMyGameObject.LoadFromStream(
const ASource: TStream);
begin
FInterestingString := ASource.ReadString;
FExampleObject.LoadFromStream(ASource);
end;
procedure TMyGameObject.SaveToFile(
const AFilename:
String);
var
FileContents: TFileStream;
begin
FileContents := TFileStream.Create(AFilename, fmCreate);
try
SaveToStream(FileContents);
finally
FileContents.Free;
end;
end;
procedure TMyGameObject.SaveToStream(
const ATarget: TStream);
begin
ATarget.WriteString(FInterestingString);
FExampleObject.SaveToStream(ATarget);
end;
procedure TMyGameObject.SetInterestingString(
const Value:
string);
begin
FInterestingString := Value;
end;
end.