2019-10-07 01:13:39 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
2020-08-18 22:10:58 +00:00
|
|
|
|
using System.Runtime.Serialization;
|
2021-10-16 18:36:13 +00:00
|
|
|
|
|
2019-10-07 01:13:39 +00:00
|
|
|
|
using CommonUtil;
|
|
|
|
|
|
|
|
|
|
namespace PluginCommon {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read-only wrapper around AddressMap.
|
|
|
|
|
///
|
|
|
|
|
/// Instance is immutable, though in theory the underlying AddressMap could change if
|
|
|
|
|
/// some other code has a reference to it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This is currently simple enough that it could just be an interface, but I don't want
|
|
|
|
|
/// to rely on that remaining true.
|
2020-03-14 01:39:11 +00:00
|
|
|
|
///
|
|
|
|
|
/// TODO(maybe): add a "IsAddressRangeValid(int srcOffset, int addr, int length)" method
|
|
|
|
|
/// that verifies an entire address range is in memory. This would allow subsequent access
|
|
|
|
|
/// to skip error checks. (You still have to do the address-to-offset translation on every
|
|
|
|
|
/// byte though, which is where most of the expense is.)
|
|
|
|
|
/// TODO(maybe): add a "CopyAddressRange(byte[] data, int srcOffset, int addr, int length)"
|
|
|
|
|
/// that returns a newly-allocated buffer with the data copied out. This would allow fast
|
|
|
|
|
/// access to data that is split into multiple regions.
|
2019-10-07 01:13:39 +00:00
|
|
|
|
/// </remarks>
|
|
|
|
|
public class AddressTranslate {
|
2021-10-16 18:36:13 +00:00
|
|
|
|
public const int NON_ADDR = AddressMap.NON_ADDR;
|
|
|
|
|
|
2019-10-07 01:13:39 +00:00
|
|
|
|
private AddressMap mAddrMap;
|
|
|
|
|
|
|
|
|
|
public AddressTranslate(AddressMap addrMap) {
|
|
|
|
|
mAddrMap = addrMap;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 22:34:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a file offset to an address.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="offset">File offset.</param>
|
|
|
|
|
/// <returns>24-bit address.</returns>
|
|
|
|
|
public int OffsetToAddress(int offset) {
|
|
|
|
|
return mAddrMap.OffsetToAddress(offset);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 01:13:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines the file offset that best contains the specified target address.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="srcOffset">Offset of the address reference. Only matters when
|
|
|
|
|
/// multiple file offsets map to the same address.</param>
|
|
|
|
|
/// <param name="targetAddr">Address to look up.</param>
|
|
|
|
|
/// <returns>The file offset, or -1 if the address falls outside the file.</returns>
|
|
|
|
|
public int AddressToOffset(int srcOffset, int targetAddr) {
|
|
|
|
|
return mAddrMap.AddressToOffset(srcOffset, targetAddr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-13 22:34:34 +00:00
|
|
|
|
/// Returns the data found at the specified address. If the address is out
|
|
|
|
|
/// of bounds this throws an AddressException.
|
2019-10-07 01:13:39 +00:00
|
|
|
|
/// </summary>
|
2020-03-13 22:34:34 +00:00
|
|
|
|
/// <param name="data">Data array.</param>
|
|
|
|
|
/// <param name="srcOffset">Offset of the address reference. Only matters when
|
|
|
|
|
/// multiple file offsets map to the same address.</param>
|
|
|
|
|
/// <param name="address">Data address.</param>
|
|
|
|
|
/// <returns>Data found.</returns>
|
|
|
|
|
public byte GetDataAtAddress(byte[] data, int srcOffset, int address) {
|
|
|
|
|
int offset = AddressToOffset(srcOffset, address);
|
|
|
|
|
if (offset == -1) {
|
|
|
|
|
Exception ex = new AddressTranslateException("Address $" + address.ToString("X4") +
|
|
|
|
|
" is outside the file bounds");
|
|
|
|
|
ex.Data.Add("Address", address);
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
byte foo = data[offset];
|
Switch to left-handed coordinate system
There's no "standard" coordinate system, so the choice is arbitrary.
However, an examination of the Transporter mesh in Elite revealed
that the mesh was designed for a left-handed coordinate system. We
can compensate for that trivially in the Elite visualizer, but we
might as well match what they're doing. (The only change required
in the code is a couple of sign changes on the Z coordinate, and an
update to the rotation matrix.)
This also downsizes Matrix44 to Matrix33, exposes the rotation mode
enum, and adds a left-handed ZYX rotation mode.
This does mean that meshes that put the front at +Z will show their
backsides initially, since we're now oriented as if we're flying
the ships rather than facing them. I considered adding a 180-degree
Y rotation (with a tweak to the rotation matrix handedness to correct
the first rotation axis) to have them facing by default, but figured
that might be confusing since +Z is supposed to be away.
Anybody who really wants it to be the other way can trivially flip
the coordinates in their visualizer (negate xc/zc).
The Z coordinates in the visualization test project were flipped so
that the design is still facing the viewer at rotation (0,0,0).
2020-03-14 17:25:17 +00:00
|
|
|
|
} catch (Exception) {
|
2020-03-13 22:34:34 +00:00
|
|
|
|
throw new AddressTranslateException("FAILED at srcOff=$" + srcOffset.ToString("x4") +
|
|
|
|
|
" addr=$" + address.ToString("x4"));
|
|
|
|
|
}
|
|
|
|
|
return data[offset];
|
2019-10-07 01:13:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 22:34:34 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Exception thrown by AddressTranslate's GetDataAtAddress().
|
|
|
|
|
/// </summary>
|
2020-08-18 22:10:58 +00:00
|
|
|
|
[Serializable]
|
2020-03-13 22:34:34 +00:00
|
|
|
|
public class AddressTranslateException : Exception {
|
|
|
|
|
public AddressTranslateException() : base() { }
|
|
|
|
|
public AddressTranslateException(string msg) : base(msg) { }
|
2020-08-18 22:10:58 +00:00
|
|
|
|
|
|
|
|
|
protected AddressTranslateException(SerializationInfo info, StreamingContext context) :
|
|
|
|
|
base(info, context) {
|
|
|
|
|
// base class handles everything
|
|
|
|
|
}
|
2020-03-13 22:34:34 +00:00
|
|
|
|
}
|
2019-10-07 01:13:39 +00:00
|
|
|
|
}
|