2018-07-28 21:01:19 -05:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2018-08-05 07:30:14 -05:00
using System.Linq ;
2018-07-28 21:01:19 -05:00
using ACE.Database.Models.World ;
namespace ACE.Database.SQLFormatters.World
{
2018-08-04 14:24:52 -05:00
public class LandblockInstanceWriter : SQLWriter
2018-07-28 21:01:19 -05:00
{
/// <summary>
/// Default is formed from: (input.ObjCellId >> 16).ToString("X4")
/// </summary>
2018-08-04 14:24:52 -05:00
public string GetDefaultFileName ( LandblockInstance input )
2018-07-28 21:01:19 -05:00
{
string fileName = ( input . ObjCellId > > 16 ) . ToString ( "X4" ) ;
fileName = IllegalInFileName . Replace ( fileName , "_" ) ;
fileName + = ".sql" ;
return fileName ;
}
2018-08-04 14:24:52 -05:00
public void CreateSQLDELETEStatement ( IList < LandblockInstance > input , StreamWriter writer )
2018-07-28 21:01:19 -05:00
{
2020-01-17 04:39:39 -05:00
writer . WriteLine ( $"DELETE FROM `landblock_instance` WHERE `landblock` = 0x{(input[0].ObjCellId >> 16):X4};" ) ;
2018-07-28 21:01:19 -05:00
}
/// <exception cref="System.Exception">WeenieClassNames must be set, and must have a record for input.ClassId.</exception>
2018-08-04 14:24:52 -05:00
public void CreateSQLINSERTStatement ( IList < LandblockInstance > input , StreamWriter writer )
2018-07-28 21:01:19 -05:00
{
2020-03-07 23:24:57 -05:00
var instanceWcids = input . ToDictionary ( i = > i . Guid , i = > i . WeenieClassId ) ;
2018-08-05 07:30:14 -05:00
input = input . OrderBy ( r = > r . Guid ) . ToList ( ) ;
2018-07-28 21:01:19 -05:00
2018-08-05 07:30:14 -05:00
foreach ( var value in input )
2018-07-28 21:01:19 -05:00
{
2018-08-05 07:30:14 -05:00
if ( value ! = input [ 0 ] )
writer . WriteLine ( ) ;
2019-02-04 11:55:13 -06:00
writer . WriteLine ( "INSERT INTO `landblock_instance` (`guid`, `weenie_Class_Id`, `obj_Cell_Id`, `origin_X`, `origin_Y`, `origin_Z`, `angles_W`, `angles_X`, `angles_Y`, `angles_Z`, `is_Link_Child`, `last_Modified`)" ) ;
2018-08-05 07:30:14 -05:00
2018-07-28 21:01:19 -05:00
string label = null ;
if ( WeenieNames ! = null )
2018-08-05 07:30:14 -05:00
WeenieNames . TryGetValue ( value . WeenieClassId , out label ) ;
var output = "VALUES (" +
2020-01-17 04:39:39 -05:00
$"0x{value.Guid.ToString(" X8 ")}, " +
2018-08-05 07:30:14 -05:00
$"{value.WeenieClassId.ToString().PadLeft(5)}, " +
2020-01-17 04:39:39 -05:00
$"0x{value.ObjCellId:X8}, " +
2021-11-08 00:41:17 -05:00
$"{TrimNegativeZero(value.OriginX):0.######}, " +
$"{TrimNegativeZero(value.OriginY):0.######}, " +
$"{TrimNegativeZero(value.OriginZ):0.######}, " +
$"{TrimNegativeZero(value.AnglesW):0.######}, " +
$"{TrimNegativeZero(value.AnglesX):0.######}, " +
$"{TrimNegativeZero(value.AnglesY):0.######}, " +
$"{TrimNegativeZero(value.AnglesZ):0.######}, " +
2019-02-04 11:55:13 -06:00
$"{value.IsLinkChild.ToString().PadLeft(5)}, " +
2019-04-10 20:48:34 -05:00
$"'{value.LastModified:yyyy-MM-dd HH:mm:ss}'" +
2018-10-09 22:54:25 -04:00
$"); /* {label} */" +
2021-11-08 00:41:17 -05:00
Environment . NewLine + $"/* @teleloc 0x{value.ObjCellId:X8} [{TrimNegativeZero(value.OriginX):F6} {TrimNegativeZero(value.OriginY):F6} {TrimNegativeZero(value.OriginZ):F6}] {TrimNegativeZero(value.AnglesW):F6} {TrimNegativeZero(value.AnglesX):F6} {TrimNegativeZero(value.AnglesY):F6} {TrimNegativeZero(value.AnglesZ):F6} */" ;
2018-08-05 07:30:14 -05:00
output = FixNullFields ( output ) ;
writer . WriteLine ( output ) ;
if ( value . LandblockInstanceLink ! = null & & value . LandblockInstanceLink . Count > 0 )
{
writer . WriteLine ( ) ;
2020-03-07 23:24:57 -05:00
CreateSQLINSERTStatement ( value . LandblockInstanceLink . OrderBy ( r = > r . ChildGuid ) . ToList ( ) , instanceWcids , writer ) ;
2018-08-05 07:30:14 -05:00
}
}
}
2020-03-07 23:24:57 -05:00
private void CreateSQLINSERTStatement ( IList < LandblockInstanceLink > input , Dictionary < uint , uint > instanceWcids , StreamWriter writer )
2018-08-05 07:30:14 -05:00
{
2019-02-04 11:55:13 -06:00
writer . WriteLine ( "INSERT INTO `landblock_instance_link` (`parent_GUID`, `child_GUID`, `last_Modified`)" ) ;
2018-08-05 07:30:14 -05:00
var lineGenerator = new Func < int , string > ( i = >
{
string label = null ;
2025-08-05 00:24:11 -04:00
//if (WeenieNames != null && instanceWcids.TryGetValue(input[i].ParentGuid, out var parentWcid) && WeenieNames.TryGetValue(parentWcid, out var parentWeenieName))
//label = $"{parentWeenieName} ({parentWcid})";
2025-07-15 21:34:45 -07:00
2020-03-07 23:24:57 -05:00
if ( WeenieNames ! = null & & instanceWcids . TryGetValue ( input [ i ] . ChildGuid , out var wcid ) & & WeenieNames . TryGetValue ( wcid , out var weenieName ) )
2025-08-05 13:46:01 -04:00
{
if ( label ! = null )
label + = $", {weenieName} ({wcid})" ;
else
label = $"{weenieName} ({wcid})" ;
}
if ( label ! = null )
{
2025-07-15 21:34:45 -07:00
label = $" /* {label} */" ;
2025-08-05 13:46:01 -04:00
}
2018-08-05 07:30:14 -05:00
2020-03-07 23:24:57 -05:00
return $"0x{input[i].ParentGuid.ToString(" X8 ")}, 0x{input[i].ChildGuid.ToString(" X8 ")}, '{input[i].LastModified.ToString(" yyyy - MM - dd HH : mm : ss ")}'){label}" ;
2018-07-28 21:01:19 -05:00
} ) ;
ValuesWriter ( input . Count , lineGenerator , writer ) ;
}
}
}