ModernUO/Projects/Scripts/Items/Books/BaseBook.cs
Kamron Batman 179cb50557 Updates Packets & Randomizer (#43)
* Fixes a bug with the main loop. Removes unnecessary optimizations for RDRand.

* WIP - Rewriting packets.

* Cleanup and updates README

* Converts more packets

* Fixes header

* Converts Effects packets.

* Forgot the send command

* Adds the start of containers packets.

* Adds message packets with caching

* Extends the send methods

* Starts to add Acquire methods

* Converted the packets

* Cleanup

* Cleanup

* Adds more packets

* Adds more packets

* Fixes clearing arrays from pool. Adds Mobile Incoming

* Converts more packets.

* Moves more packets

* Moves more packets

* Test compression

* Merges

* Migrating to an idiomatic syntax that also supports compression, and proxying.

* Optimize the stack alloc. Changes attributes

* Converted container packets

* Visual Studio doesn't auto save files. I am still getting used to subpar IDEs.

* Adds static packet caching. Will profile later. Converts more packets

* Fixes packets and changes how compression is configured

* WIP - Adds basics for Gumps. Not finished though.

* Fix file

* Fixes formatting

* Changed SpanWriter to be more idiomatic.

* Fixes Span vs RawSpan and missing stackallocs

* Converts over some more gumps

* WIP - Deletes 32bit support.

* Drops RDRand32 support.

* Fixes gump compilation

* Converting gump components

* Removes old huffman compression function

* Revert signature for backwards compatibility

* WIP - Converting more gump components

* Creates ArraySet for the strings. Updates AppendTo to reference that.

* Converts the maining gump components

* Cleans up gump components

* Cleans up directives

* Cleans up ArraySet and moves it. Adds null-coalescing-assignment

* Cleanup, Target Packets, and C# 8 changes.

* Removed OPL Packet

* World packets

* Fixing packet uses

* Cleans up code. Fixes packet uses in various places.

* More cleanup for packets

* Code cleanup

* Converts more packet uses and cleans up more code

* More code cleanup

* Finishes fixing the packets in Item

* Updates secure trade packets

* Updates core and gets it to compile.

* Moved packets to scripts. Fixed account handler use of packets

* Code cleanup

* Updates chat packets

* Code cleanuo

* Adds party packets, but need to implement them.

* Party packets WIP

* Finishes party packets

* Rearrange movement namespaces and classes

* Finishes plant packets

* Code Formatting

* Fixes moving effects

* Code cleanup, eliminates equipinfo

* Adds more packets. Fixes bugs with various packets.

* Finishes mahjon packets

* Fixes mahjong packet

* Code cleanup

* Finishes mahjong packets

* Adds Map packets

* Add multifacet maps and charts

* Cleans up some packets with UTF8

* Optimizes packets

* Cleans up more packets. Moves the MessageHelper

* Removes assistant support. Removes extended protocol. Incorporates MapUO packets as normal packets.

* Updates protocol extensions packet receiver

* Fixes a few bugs. Fixes a few more packets.

* Code cleanup and fixing more packets

* Fixes packet effects

* Cleaned up more code

* Buff Icon cleanup

* Removed unused constructors

* Code Cleanup. Adds BoatHS Packets

* Moves house files. Updates house foundation packets.

* Deployment cleanup

* Fixes:

* Code cleanup

* More code cleanup

* More code cleanup

* Cleaned up BaseHouse

* Enforces styling

* Converts foreach to linq where possible.

* Dont need that

* Goals/Readme updates

* Removes 32bit support at the highest level. Turns on HRT by default.

* Code cleanup. Fixes extended features packet.

* Fixes various bugs

* Code cleanup

* Code cleanup

* Code cleanup. Fixes gump X/Y assignment.

* Code cleanup using |= operator

* More code cleanup

* Cleanup

* Fixes spacing issues. Thanks Visual Studio. You suck.

* Compiler error

* Fixes NPE from RunUO 2.7

* Renames ScriptCompiler to AssemblyHandler. Fixes packets. Updates README

* Fixes more packets. Stupid trailing nulls.

* Fixes various bugs.

* Fixes for gumps

* Fixes more gump stuff. Going to split it out later since it is getting insane

* Recoded the gump writing

* WIP

* *Added output path of scripts project to dev branch
*Activated debugging in code
2019-11-11 12:40:16 +01:00

399 lines
9.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Server.ContextMenus;
using Server.Gumps;
using Server.Multis;
using Server.Network;
namespace Server.Items
{
public class BookPageInfo
{
public BookPageInfo() => Lines = new string[0];
public BookPageInfo(params string[] lines) => Lines = lines;
public BookPageInfo(GenericReader reader)
{
int length = reader.ReadInt();
Lines = new string[length];
for (int i = 0; i < Lines.Length; ++i)
Lines[i] = Utility.Intern(reader.ReadString());
}
public string[] Lines{ get; set; }
public void Serialize(GenericWriter writer)
{
writer.Write(Lines.Length);
for (int i = 0; i < Lines.Length; ++i)
writer.Write(Lines[i]);
}
}
public class BaseBook : Item, ISecurable
{
private string m_Author;
private string m_Title;
[Constructible]
public BaseBook(int itemID, int pageCount = 20, bool writable = true) : this(itemID, null, null, pageCount, writable)
{
}
[Constructible]
public BaseBook(int itemID, string title, string author, int pageCount, bool writable) : base(itemID)
{
BookContent content = DefaultContent;
m_Title = title ?? content?.Title;
m_Author = author ?? content?.Author;
Writable = writable;
if (content == null)
{
Pages = new BookPageInfo[Math.Min(pageCount, 255)];
for (int i = 0; i < Pages.Length; ++i)
Pages[i] = new BookPageInfo();
}
else
Pages = content.Copy();
}
// Intended for defined books only
public BaseBook(int itemID, bool writable) : this(itemID, 0, writable)
{
}
public BaseBook(Serial serial) : base(serial)
{
}
[CommandProperty(AccessLevel.GameMaster)]
public string Title
{
get => m_Title;
set
{
m_Title = value;
InvalidateProperties();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public string Author
{
get => m_Author;
set
{
m_Author = value;
InvalidateProperties();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public bool Writable{ get; set; }
[CommandProperty(AccessLevel.GameMaster)]
public int PagesCount => Pages.Length;
public BookPageInfo[] Pages{ get; private set; }
public virtual BookContent DefaultContent => null;
public string ContentAsString
{
get
{
StringBuilder sb = new StringBuilder();
foreach (BookPageInfo bpi in Pages)
foreach (string line in bpi.Lines)
sb.AppendLine(line);
return sb.ToString();
}
}
public string[] ContentAsStringArray
{
get
{
List<string> lines = new List<string>();
foreach (BookPageInfo bpi in Pages) lines.AddRange(bpi.Lines);
return lines.ToArray();
}
}
#region ISecurable Members
[CommandProperty(AccessLevel.GameMaster)]
public SecureLevel Level{ get; set; }
#endregion
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
SetSecureLevelEntry.AddTo(from, this, list);
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
BookContent content = DefaultContent;
SaveFlags flags = SaveFlags.None;
if (m_Title != content?.Title)
flags |= SaveFlags.Title;
if (m_Author != content?.Author)
flags |= SaveFlags.Author;
if (Writable)
flags |= SaveFlags.Writable;
if (content?.IsMatch(Pages) != true)
flags |= SaveFlags.Content;
writer.Write(4); // version
writer.Write((int)Level);
writer.Write((byte)flags);
if ((flags & SaveFlags.Title) != 0)
writer.Write(m_Title);
if ((flags & SaveFlags.Author) != 0)
writer.Write(m_Author);
if ((flags & SaveFlags.Content) != 0)
{
writer.WriteEncodedInt(Pages.Length);
for (int i = 0; i < Pages.Length; ++i)
Pages[i].Serialize(writer);
}
}
public override void Deserialize(GenericReader reader)
{
base.Deserialize(reader);
int version = reader.ReadInt();
switch (version)
{
case 4:
{
Level = (SecureLevel)reader.ReadInt();
goto case 3;
}
case 3:
case 2:
{
BookContent content = DefaultContent;
SaveFlags flags = (SaveFlags)reader.ReadByte();
if ((flags & SaveFlags.Title) != 0)
m_Title = Utility.Intern(reader.ReadString());
else if (content != null)
m_Title = content.Title;
if ((flags & SaveFlags.Author) != 0)
m_Author = reader.ReadString();
else if (content != null)
m_Author = content.Author;
Writable = (flags & SaveFlags.Writable) != 0;
if ((flags & SaveFlags.Content) != 0)
{
Pages = new BookPageInfo[reader.ReadEncodedInt()];
for (int i = 0; i < Pages.Length; ++i)
Pages[i] = new BookPageInfo(reader);
}
else
Pages = content?.Copy() ?? new BookPageInfo[0];
break;
}
case 1:
case 0:
{
m_Title = reader.ReadString();
m_Author = reader.ReadString();
Writable = reader.ReadBool();
if (version == 0 || reader.ReadBool())
{
Pages = new BookPageInfo[reader.ReadInt()];
for (int i = 0; i < Pages.Length; ++i)
Pages[i] = new BookPageInfo(reader);
}
else
Pages = DefaultContent?.Copy() ?? new BookPageInfo[0];
break;
}
}
if (version < 3 && (Weight == 1 || Weight == 2))
Weight = -1;
}
public override void AddNameProperty(ObjectPropertyList list)
{
if (!string.IsNullOrEmpty(m_Title))
list.Add(m_Title);
else
base.AddNameProperty(list);
}
/*public override void GetProperties( ObjectPropertyList list )
{
base.GetProperties( list );
if ( m_Title?.Length > 0 )
list.Add( 1060658, "Title\t{0}", m_Title ); // ~1_val~: ~2_val~
if ( m_Author?.Length > 0 )
list.Add( 1060659, "Author\t{0}", m_Author ); // ~1_val~: ~2_val~
if ( m_Pages?.Length > 0 )
list.Add( 1060660, "Pages\t{0}", m_Pages.Length ); // ~1_val~: ~2_val~
}*/
public override void OnSingleClick(Mobile from)
{
LabelTo(from, "{0} by {1}", m_Title, m_Author);
LabelTo(from, "[{0} pages]", Pages.Length);
}
public override void OnDoubleClick(Mobile from)
{
if (m_Title == null && m_Author == null && Writable)
{
Title = "a book";
Author = from.Name;
}
BookPackets.SendBookHeader(from.NetState, from, this);
BookPackets.SendBookPageDetails(from.NetState, this);
}
public static void Initialize()
{
PacketHandlers.Register(0xD4, 0, true, HeaderChange);
PacketHandlers.Register(0x66, 0, true, ContentChange);
PacketHandlers.Register(0x93, 99, true, OldHeaderChange);
}
public static void OldHeaderChange(NetState state, PacketReader pvSrc)
{
Mobile from = state.Mobile;
if (!(World.FindItem(pvSrc.ReadUInt32()) is BaseBook book) || !book.Writable ||
!from.InRange(book.GetWorldLocation(), 1) || !book.IsAccessibleTo(from))
return;
pvSrc.Seek(4, SeekOrigin.Current); // Skip flags and page count
string title = pvSrc.ReadStringSafe(60);
string author = pvSrc.ReadStringSafe(30);
book.Title = Utility.FixHtml(title);
book.Author = Utility.FixHtml(author);
}
public static void HeaderChange(NetState state, PacketReader pvSrc)
{
Mobile from = state.Mobile;
if (!(World.FindItem(pvSrc.ReadUInt32()) is BaseBook book) || !book.Writable ||
!from.InRange(book.GetWorldLocation(), 1) || !book.IsAccessibleTo(from))
return;
pvSrc.Seek(4, SeekOrigin.Current); // Skip flags and page count
int titleLength = pvSrc.ReadUInt16();
if (titleLength > 60)
return;
string title = pvSrc.ReadStringSafe(titleLength);
int authorLength = pvSrc.ReadUInt16();
if (authorLength > 30)
return;
string author = pvSrc.ReadStringSafe(authorLength);
book.Title = Utility.FixHtml(title);
book.Author = Utility.FixHtml(author);
}
public static void ContentChange(NetState state, PacketReader pvSrc)
{
Mobile from = state.Mobile;
if (!(World.FindItem(pvSrc.ReadUInt32()) is BaseBook book) || !book.Writable ||
!from.InRange(book.GetWorldLocation(), 1) || !book.IsAccessibleTo(from))
return;
int pageCount = pvSrc.ReadUInt16();
if (pageCount > book.PagesCount)
return;
for (int i = 0; i < pageCount; ++i)
{
int index = pvSrc.ReadUInt16();
if (index < 1 || index > book.PagesCount)
return;
--index;
int lineCount = pvSrc.ReadUInt16();
if (lineCount > 8)
return;
string[] lines = new string[lineCount];
for (int j = 0; j < lineCount; ++j)
if ((lines[j] = pvSrc.ReadUTF8StringSafe()).Length >= 80)
return;
book.Pages[index].Lines = lines;
}
}
[Flags]
private enum SaveFlags
{
None = 0x00,
Title = 0x01,
Author = 0x02,
Writable = 0x04,
Content = 0x08
}
}
}