Skip to content

Commit

Permalink
Made the GameStrng and Shared more opaque
Browse files Browse the repository at this point in the history
  • Loading branch information
TCA166 committed May 14, 2024
1 parent 8296e56 commit 3918f08
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 83 deletions.
40 changes: 39 additions & 1 deletion src/game_object.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
use std::{collections::{hash_map, HashMap}, rc::Rc, slice, fmt::Debug};
use std::{cell::{BorrowError, BorrowMutError, Ref, RefMut}, collections::{hash_map, HashMap}, fmt::Debug, ops::Deref, rc::Rc, slice};

/// A reference or a raw value. I have no clue why this isn't a standard library type.
/// A [Ref] and a raw reference are both dereferencable to the same type.
pub enum RefOrRaw<'a, T: 'a> {
Ref(Ref<'a, T>),
Raw(&'a T),
}

impl<'a, T> Deref for RefOrRaw<'a, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
match self {
RefOrRaw::Ref(r) => r.deref(),
RefOrRaw::Raw(r) => r,
}
}
}

/// A trait for objects that wrap a certain value.
/// Allows us to create opaque type aliases for certain types.
/// For example [GameString] is a wrapper around a reference counted string that implements this trait meaning if we wanted to change how the reference counting works we can do it with no interface changes.
pub trait Wrapper<T> {
/// Wrap a value in the object
fn wrap(t:T) -> Self;

fn get_internal(&self) -> RefOrRaw<T>;

fn try_get_internal(&self) -> Result<RefOrRaw<T>, BorrowError>;
}

/// A trait for objects that wrap a certain value and allow mutation.
/// Allows us to create opaque type aliases for certain types.
pub trait WrapperMut<T> {
fn get_internal_mut(&self) -> RefMut<T>;

fn try_get_internal_mut(&self) -> Result<RefMut<T>, BorrowMutError>;
}

/// A type alias for a game object id.
Expand All @@ -21,6 +51,14 @@ impl Wrapper<String> for GameString {
fn wrap(t:String) -> Self {
Rc::new(t)
}

fn get_internal(&self) -> RefOrRaw<String>{
RefOrRaw::Raw(self.as_ref())
}

fn try_get_internal(&self) -> Result<RefOrRaw<String>, BorrowError> {
Ok(self.get_internal())
}
}

/// A value that can be stored in a SaveFile and is held by a GameObject.
Expand Down
16 changes: 8 additions & 8 deletions src/game_state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use crate::structures::{Character, Culture, DerivedRef, Dynasty, Faith, GameObjectDerived, Memory, Shared, Title};
use crate::game_object::{GameId, GameObject, GameString, Wrapper};
use crate::game_object::{GameId, GameObject, GameString, Wrapper, WrapperMut};

/// A struct representing all known game objects.
/// It is guaranteed to always return a reference to the same object for the same key.
Expand Down Expand Up @@ -80,7 +80,7 @@ impl GameState{
let char = self.get_character(character_id);
if self.contract_transform.contains_key(contract_id){
let entry = self.contract_transform.get(contract_id).unwrap();
entry.borrow_mut().init(char);
entry.get_internal_mut().init(char);
}
else{
let r = Shared::wrap(DerivedRef::from_derived(char));
Expand Down Expand Up @@ -153,7 +153,7 @@ impl GameState{
let key = value.get_name().parse::<GameId>().unwrap();
if self.characters.contains_key(&key){
let c = self.characters.get(&key).unwrap().clone();
c.borrow_mut().init(value, self);
c.get_internal_mut().init(value, self);
}
else{
let c = Character::from_game_object(value, self);
Expand All @@ -166,7 +166,7 @@ impl GameState{
let key = value.get_name().parse::<GameId>().unwrap();
if self.titles.contains_key(&key){
let t = self.titles.get(&key).unwrap().clone();
t.borrow_mut().init(value, self);
t.get_internal_mut().init(value, self);
}
else{
let t = Title::from_game_object(value, self);
Expand All @@ -179,7 +179,7 @@ impl GameState{
let key = value.get_name().parse::<GameId>().unwrap();
if self.faiths.contains_key(&key){
let f = self.faiths.get(&key).unwrap().clone();
f.borrow_mut().init(value, self);
f.get_internal_mut().init(value, self);
}
else{
let f = Faith::from_game_object(value, self);
Expand All @@ -192,7 +192,7 @@ impl GameState{
let key = value.get_name().parse::<GameId>().unwrap();
if self.cultures.contains_key(&key){
let c = self.cultures.get(&key).unwrap().clone();
c.borrow_mut().init(value, self);
c.get_internal_mut().init(value, self);
}
else{
let c = Culture::from_game_object(value, self);
Expand All @@ -205,7 +205,7 @@ impl GameState{
let key = value.get_name().parse::<GameId>().unwrap();
if self.dynasties.contains_key(&key){
let d = self.dynasties.get(&key).unwrap().clone();
d.borrow_mut().init(value, self);
d.get_internal_mut().init(value, self);
}
else{
let d = Dynasty::from_game_object(value, self);
Expand All @@ -218,7 +218,7 @@ impl GameState{
let key = value.get_name().parse::<GameId>().unwrap();
if self.memories.contains_key(&key){
let m = self.memories.get(&key).unwrap().clone();
m.borrow_mut().init(value, self);
m.get_internal_mut().init(value, self);
}
else{
let m = Memory::from_game_object(value, self);
Expand Down
64 changes: 32 additions & 32 deletions src/structures/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use minijinja::context;

use serde::{Serialize, ser::SerializeStruct};

use crate::{game_object::{GameObject, GameString, SaveFileValue, Wrapper}, game_state::GameState};
use crate::{game_object::{GameObject, GameString, SaveFileValue, Wrapper, WrapperMut}, game_state::GameState};

use super::{renderer::Renderable, serialize_array, Cullable, Culture, DerivedRef, Dynasty, Faith, GameId, GameObjectDerived, Memory, Shared, Title};

Expand Down Expand Up @@ -118,15 +118,15 @@ fn get_family(self_id:GameId, spouses:&mut Vec<Shared<Character>>, former_spouse
SaveFileValue::Object(o) => {
for s in o.get_array_iter(){
let c = game_state.get_character(&s.as_id()).clone();
let contains = former_spouses.iter().any(|x| x.as_ref().borrow().get_id() == c.as_ref().borrow().get_id());
let contains = former_spouses.iter().any(|x| x.get_internal().get_id() == c.get_internal().get_id());
if !contains{
spouses.push(c);
}
}
}
SaveFileValue::String(o) => {
let c = game_state.get_character(&o.parse::<GameId>().unwrap()).clone();
let contains = former_spouses.iter().any(|x| x.as_ref().borrow().get_id() == c.as_ref().borrow().get_id());
let contains = former_spouses.iter().any(|x| x.get_internal().get_id() == c.get_internal().get_id());
if !contains{
spouses.push(c);
}
Expand All @@ -136,8 +136,8 @@ fn get_family(self_id:GameId, spouses:&mut Vec<Shared<Character>>, former_spouse
let primary_spouse_node = f.get("primary_spouse");
if primary_spouse_node.is_some() {
let c = game_state.get_character(&primary_spouse_node.unwrap().as_id()).clone();
let mut contains = former_spouses.iter().any(|x| x.as_ref().borrow().get_id() == c.as_ref().borrow().get_id());
contains = contains || spouses.iter().any(|x| x.as_ref().borrow().get_id() == c.as_ref().borrow().get_id());
let mut contains = former_spouses.iter().any(|x| x.get_internal().get_id() == c.get_internal().get_id());
contains = contains || spouses.iter().any(|x| x.get_internal().get_id() == c.get_internal().get_id());
if !contains{
spouses.push(c);
}
Expand All @@ -147,7 +147,7 @@ fn get_family(self_id:GameId, spouses:&mut Vec<Shared<Character>>, former_spouse
let parent = game_state.get_character(&self_id);
for s in children_node.unwrap().as_object().unwrap().get_array_iter(){
let c = game_state.get_character(&s.as_id()).clone();
c.borrow_mut().register_parent(parent.clone());
c.get_internal_mut().register_parent(parent.clone());
children.push(c);
}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ fn get_dynasty(base:&GameObject, game_state:&mut GameState) -> Option<Shared<Dyn
let dynasty_id = base.get("dynasty_house");
if dynasty_id.is_some(){
let d = game_state.get_dynasty(&dynasty_id.unwrap().as_id());
d.borrow_mut().register_member();
d.get_internal_mut().register_member();
return Some(d);
}
None
Expand Down Expand Up @@ -436,11 +436,11 @@ impl Serialize for Character {
let rd = DerivedRef::<Dynasty>::from_derived(h.clone());
state.serialize_field("house", &rd)?;
if faith.is_none(){
let o = h.as_ref().borrow();
let o = h.get_internal();
faith = o.get_faith();
}
if culture.is_none(){
let o = h.as_ref().borrow();
let o = h.get_internal();
culture = o.get_culture();
}
}
Expand Down Expand Up @@ -507,37 +507,37 @@ impl Renderable for Character {
return;
}
if self.faith.is_some(){
self.faith.as_ref().unwrap().as_ref().borrow().render_all(renderer);
self.faith.as_ref().unwrap().get_internal().render_all(renderer);
}
if self.culture.is_some(){
self.culture.as_ref().unwrap().as_ref().borrow().render_all(renderer);
self.culture.as_ref().unwrap().get_internal().render_all(renderer);
}
if self.house.is_some(){
self.house.as_ref().unwrap().as_ref().borrow().render_all(renderer);
self.house.as_ref().unwrap().get_internal().render_all(renderer);
}
for s in self.spouses.iter(){
s.as_ref().borrow().render_all(renderer);
s.get_internal().render_all(renderer);
}
for s in self.former.iter(){
s.as_ref().borrow().render_all(renderer);
s.get_internal().render_all(renderer);
}
for s in self.children.iter(){
s.as_ref().borrow().render_all(renderer);
s.get_internal().render_all(renderer);
}
for s in self.parents.iter(){
s.as_ref().borrow().render_all(renderer);
s.get_internal().render_all(renderer);
}
for s in self.kills.iter(){
s.as_ref().borrow().render_all(renderer);
s.get_internal().render_all(renderer);
}
for s in self.vassals.iter(){
s.as_ref().borrow().get_ref().as_ref().borrow().render_all(renderer);
s.get_internal().get_ref().get_internal().render_all(renderer);
}
for s in self.titles.iter(){
s.as_ref().borrow().render_all(renderer);
s.get_internal().render_all(renderer);
}
for m in self.memories.iter() {
m.as_ref().borrow().render_participants(renderer);
m.get_internal().render_participants(renderer);
}
}
}
Expand All @@ -549,64 +549,64 @@ impl Cullable for Character {
}
self.depth = depth;
for s in self.spouses.iter(){
let o = s.try_borrow_mut();
let o = s.try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
}
for s in self.former.iter(){
let o = s.try_borrow_mut();
let o = s.try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
}
for s in self.children.iter(){
let o = s.try_borrow_mut();
let o = s.try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
}
for s in self.parents.iter(){
let o = s.try_borrow_mut();
let o = s.try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
}
for s in self.kills.iter(){
let o = s.try_borrow_mut();
let o = s.try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
}
for s in self.vassals.iter(){
let o = s.try_borrow_mut();
let o = s.try_get_internal_mut();
if o.is_ok(){
o.unwrap().get_ref().as_ref().borrow_mut().set_depth(depth - 1);
o.unwrap().get_ref().get_internal_mut().set_depth(depth - 1);
}
}
if self.culture.is_some(){
let o = self.culture.as_ref().unwrap().try_borrow_mut();
let o = self.culture.as_ref().unwrap().try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
}
if self.faith.is_some(){
let o = self.faith.as_ref().unwrap().try_borrow_mut();
let o = self.faith.as_ref().unwrap().try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
}
for s in self.titles.iter(){
let o = s.try_borrow_mut();
let o = s.try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
}
for s in self.memories.iter(){
s.borrow_mut().set_depth(depth - 1);
s.get_internal_mut().set_depth(depth - 1);
}
if self.house.is_some(){
let o = self.house.as_ref().unwrap().try_borrow_mut();
let o = self.house.as_ref().unwrap().try_get_internal_mut();
if o.is_ok(){
o.unwrap().set_depth(depth - 1);
}
Expand Down
6 changes: 3 additions & 3 deletions src/structures/culture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Serialize;
use serde::ser::SerializeStruct;
use super::renderer::Renderable;
use super::{serialize_array, Cullable, GameId, GameObjectDerived, Shared};
use crate::game_object::{GameObject, GameString, Wrapper};
use crate::game_object::{GameObject, GameString, Wrapper, WrapperMut};

/// A struct representing a culture in the game
pub struct Culture {
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Renderable for Culture {
return;
}
for p in &self.parents{
p.borrow().render_all(renderer);
p.get_internal().render_all(renderer);
}
}
}
Expand All @@ -156,7 +156,7 @@ impl Cullable for Culture {
}
self.depth = depth;
for p in &self.parents{
p.borrow_mut().set_depth(depth-1);
p.get_internal_mut().set_depth(depth-1);
}
}
}
Loading

0 comments on commit 3918f08

Please sign in to comment.