Skip to content

Commit

Permalink
Merge pull request #8 from newcomb-luke/develop
Browse files Browse the repository at this point in the history
Release v4.0.1
  • Loading branch information
newcomb-luke authored Oct 27, 2022
2 parents 3a2d55c + 817a16e commit 6d601d1
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 44 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kerbalobjects"
version = "4.0.0"
version = "4.0.1"
authors = ["Luke Newcomb <[email protected]>"]
edition = "2021"
license = "GPL-3.0"
Expand All @@ -10,7 +10,6 @@ homepage = "https://github.com/newcomb-luke/kerbalobjects.rs"

[features]
default = ["ksm", "ko"]
print_debug = []
ksm = []
ko = []

Expand Down
15 changes: 13 additions & 2 deletions src/ko/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ impl KOFile {
// should be updated except for the first null section.
let mut header_set = vec![self.shstrtab.section_index()];

// Update the .shstrtab
self.section_headers
.get_mut(usize::from(self.shstrtab.section_index()))
.unwrap()
.size = self.shstrtab.size();

for i in 0..self.str_tabs.len() {
let section = self.str_tabs.get(i).unwrap();
let idx = section.section_index();
Expand Down Expand Up @@ -571,7 +577,7 @@ impl KOFile {
.map_err(KOParseError::StringTableParseError)?;

// We skip the first one here since, there is no 0th section
for section_idx in (1..section_headers.len() as u16).map(|i| SectionIdx::from(i)) {
for section_idx in (1..section_headers.len() as u16).map(SectionIdx::from) {
// We've already parsed the .shstrtab
if section_idx == header.shstrtab_idx {
continue;
Expand Down Expand Up @@ -658,6 +664,11 @@ impl WritableKOFile {

// Write out all of the sections in order
for section_index in (0..ko.section_header_count()).map(|i| SectionIdx::from(i as u16)) {
if self.0.shstrtab.section_index() == section_index {
self.0.shstrtab.write(buf);
continue;
}

if let Some(section) = ko
.str_tabs
.iter()
Expand Down Expand Up @@ -827,7 +838,7 @@ macro_rules! gen_get_by_name {
macro_rules! gen_new_section {
($(#[$attr:meta])* => $func_name: ident, $section_type: ty, $section_kind: expr) => {
$(#[$attr])*
pub fn $func_name(&mut self, name: &str) -> $section_type {
pub fn $func_name(&mut self, name: impl Into<String>) -> $section_type {
let sh_index = self.new_section_header(name, $section_kind);
<$section_type>::with_capacity(4, sh_index)
}
Expand Down
22 changes: 22 additions & 0 deletions src/ko/sections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,28 @@ mod tests {
assert_eq!(strs.next().unwrap(), "world");
}

#[test]
fn strtab_read_write() {
let mut strtab = StringTable::with_capacity(2, SectionIdx::NULL);

strtab.add("strings");
strtab.add("in");
strtab.add("rust");

let mut buffer = Vec::new();

strtab.write(&mut buffer);

let mut iterator = BufferIterator::new(&buffer);

let read_strtab =
StringTable::parse(&mut iterator, strtab.size(), strtab.section_index()).unwrap();

for (s1, s2) in strtab.strings().zip(read_strtab.strings()) {
assert_eq!(s1, s2);
}
}

#[test]
fn symtab_insert() {
use crate::ko::symbols::KOSymbol;
Expand Down
9 changes: 4 additions & 5 deletions src/ko/sections/string_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl StringTable {
hasher.write(s.as_ref().as_bytes());
let hash = hasher.finish();

self.map.get(&hash).cloned().map(|v| StringIdx::from(v))
self.map.get(&hash).cloned().map(StringIdx::from)
}

// This doesn't make any sense to have, but I already wrote it
Expand Down Expand Up @@ -198,10 +198,9 @@ impl StringTable {
let mut b = Vec::new();

while read < size {
let c = source.next().ok_or(StringTableParseError::EOFError(
source.current_index(),
size,
))?;
let c = source
.next()
.ok_or_else(|| StringTableParseError::EOFError(source.current_index(), size))?;
read += 1;

if c == b'\0' {
Expand Down
8 changes: 4 additions & 4 deletions src/ko/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ impl KOSymbol {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ReldEntry {
/// The function section which this relocation applies to
section_index: SectionIdx,
pub section_index: SectionIdx,
/// The index of the instruction whose operand needs to be "relocated"
instr_index: InstrIdx,
pub instr_index: InstrIdx,
/// The operand of the instruction which needs to be "relocated"
operand_index: OperandIndex,
pub operand_index: OperandIndex,
/// The index into the symbol table which should replace this operand
symbol_index: SymbolIdx,
pub symbol_index: SymbolIdx,
}

impl ReldEntry {
Expand Down
15 changes: 0 additions & 15 deletions src/ksm/sections/argument_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,6 @@ impl ArgumentSection {
/// This can fail if the buffer runs out of bytes, or if the argument section is malformed.
///
pub fn parse(source: &mut BufferIterator) -> Result<Self, ArgumentSectionParseError> {
#[cfg(feature = "print_debug")]
{
println!("Reading ArgumentSection");
}

let header =
u16::from_bytes(source).map_err(|_| ArgumentSectionParseError::MissingHeader)?;

Expand All @@ -270,11 +265,6 @@ impl ArgumentSection {
.try_into()
.map_err(ArgumentSectionParseError::InvalidNumArgIndexBytes)?;

#[cfg(feature = "print_debug")]
{
println!("\tNumber of index bytes: {}", raw_num_index_bytes);
}

let mut arg_section = Self {
num_index_bytes,
hashes: HashMap::new(),
Expand All @@ -292,11 +282,6 @@ impl ArgumentSection {
ArgumentSectionParseError::KOSValueParseError(source.current_index(), e)
})?;

#[cfg(feature = "print_debug")]
{
println!("\tRead argument: {:?}", argument);
}

arg_section.add(argument);
}
} else {
Expand Down
15 changes: 0 additions & 15 deletions src/ksm/sections/code_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,11 @@ impl CodeSection {
source: &mut BufferIterator,
index_bytes: IntSize,
) -> Result<Self, CodeSectionParseError> {
#[cfg(feature = "print_debug")]
{
print!("Reading code section, ");
}

let raw_section_type =
u8::from_bytes(source).map_err(|_| CodeSectionParseError::MissingCodeSectionType)?;
let section_type = CodeType::try_from(raw_section_type)
.map_err(CodeSectionParseError::InvalidCodeSectionType)?;

#[cfg(feature = "print_debug")]
{
println!("{:?}", section_type);
}

let mut instructions = Vec::new();

loop {
Expand All @@ -145,11 +135,6 @@ impl CodeSection {
let instr = Instr::parse(source, index_bytes)
.map_err(CodeSectionParseError::InstrParseError)?;

#[cfg(feature = "print_debug")]
{
println!("\tRead instruction {:?}", instr);
}

instructions.push(instr);
} else {
return Err(CodeSectionParseError::EOF);
Expand Down
2 changes: 1 addition & 1 deletion tests/ksm_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ fn write_ksm() {

let mut arg_section = ArgumentSection::new();

let first_index = arg_section.add(first);
let print_index = arg_section.add(print);
let empty_index = arg_section.add(empty);
let two_index = arg_section.add(two);
let marker_index = arg_section.add(marker);
let first_index = arg_section.add(first);
let one_index = arg_section.add(one);
let zero_index = arg_section.add(zero);

Expand Down

0 comments on commit 6d601d1

Please sign in to comment.