Skip to content

Commit

Permalink
CATTY-614 Parser & Serializer for CBL 0996 (#1676)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio G authored Jul 23, 2021
1 parent 9b1ab50 commit dce344b
Show file tree
Hide file tree
Showing 68 changed files with 1,616 additions and 1,635 deletions.
69 changes: 33 additions & 36 deletions src/Catty.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ - (BOOL)isObjectUserDataEqualToElement:(GDataXMLElement*)node {

NSArray *nodeChildren = [node childrenWithoutComments];
NSUInteger nodeDataCount = 0;

if (children.count != nodeChildren.count) {
return false;
}

for (GDataXMLElement *nodeChild in nodeChildren) {
nodeDataCount += [nodeChild childWithElementName:@"list"].childCount;
Expand All @@ -302,7 +306,7 @@ - (BOOL)isObjectUserDataEqualToElement:(GDataXMLElement*)node {
}

for (GDataXMLElement *nodeChild in nodeChildren) {
if (![self isElementOrNode:child equalToElementOrNode:nodeChild]) {
if ([self isElementOrNode:child equalToElementOrNode:nodeChild]) {
found = true;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Catty/Supporting Files/App-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<key>CatrobatBuildName</key>
<string>Catty</string>
<key>CatrobatLanguageVersion</key>
<string>0.995</string>
<string>0.996</string>
<key>CatrobatMediaLicense</key>
<string>https://developer.catrobat.org/ccbysa_v4</string>
<key>CatrobatPlatformName</key>
Expand Down
5 changes: 5 additions & 0 deletions src/Catty/XML/Parser/Context/CBXMLParserContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@

- (id)parseFromElement:(GDataXMLElement*)xmlElement withClass:(Class<CBXMLNodeProtocol>)modelClass;
- (id)mutableCopy;
- (bool)isEqualToLanguageVersion:(CGFloat)languageVersion;
- (bool)isGreaterThanOrEqualToLanguageVersion:(CGFloat)languageVersion;
- (bool)isGreaterThanLanguageVersion:(CGFloat)languageVersion;
- (bool)isSmallerThanOrEqualToLanguageVersion:(CGFloat)languageVersion;
- (bool)isSmallerThanLanguageVersion:(CGFloat)languageVersion;

@end
45 changes: 45 additions & 0 deletions src/Catty/XML/Parser/Context/CBXMLParserContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,51 @@ - (id)parseFromElement:(GDataXMLElement*)xmlElement withClass:(Class<CBXMLNodePr
return nil;
}

- (bool)isEqualToLanguageVersion:(CGFloat)languageVersion
{
if (round(self.languageVersion * 10000) == round(languageVersion * 10000)) {
return YES;
} else {
return NO;
}
}

- (bool)isGreaterThanOrEqualToLanguageVersion:(CGFloat)languageVersion
{
if (round(self.languageVersion * 10000) >= round(languageVersion * 10000)) {
return YES;
} else {
return NO;
}
}

- (bool)isGreaterThanLanguageVersion:(CGFloat)languageVersion
{
if (round(self.languageVersion * 10000) > round(languageVersion * 10000)) {
return YES;
} else {
return NO;
}
}

- (bool)isSmallerThanOrEqualToLanguageVersion:(CGFloat)languageVersion
{
if (round(self.languageVersion * 10000) <= round(languageVersion * 10000)) {
return YES;
} else {
return NO;
}
}

- (bool)isSmallerThanLanguageVersion:(CGFloat)languageVersion
{
if (round(self.languageVersion * 10000) < round(languageVersion * 10000)) {
return YES;
} else {
return NO;
}
}

#pragma mark - Getters and Setters
- (NSMutableArray*)programVariableList
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ - (GDataXMLElement*)xmlElementWithContext:(CBXMLSerializerContext*)context

Formula *formulaForCatroidCompatibility = [[Formula alloc] initWithZero];
GDataXMLElement *formulaList = [GDataXMLElement elementWithName:@"formulaList" context:context];

GDataXMLElement *formula = [formulaForCatroidCompatibility xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"Y_POSITION"]];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"X_POSITION"]];
[formulaList addChild:formula context:context];

formula = [formulaForCatroidCompatibility xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"X_POSITION"]];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"Y_POSITION"]];
[formulaList addChild:formula context:context];

[brick addChild:formulaList context:context];

if (self.userVariable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ - (GDataXMLElement*)xmlElementWithContext:(CBXMLSerializerContext*)context
[index addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"INSERT_ITEM_INTO_USERLIST_INDEX"]];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"INSERT_ITEM_INTO_USERLIST_VALUE"]];

[formulaList addChild:index context:context];
[formulaList addChild:formula context:context];

[formulaList addChild:index context:context];

[brick addChild:formulaList context:context];

if (self.userList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ - (GDataXMLElement*)xmlElementWithContext:(CBXMLSerializerContext*)context
{
GDataXMLElement *brick = [super xmlElementForBrickType:@"ShowTextBrick" withContext:context];
GDataXMLElement *formulaList = [GDataXMLElement elementWithName:@"formulaList" context:context];
GDataXMLElement *formula = [self.yFormula xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"Y_POSITION"]];
[formulaList addChild:formula context:context];
formula = [self.xFormula xmlElementWithContext:context];

GDataXMLElement *formula = [self.xFormula xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"X_POSITION"]];
[formulaList addChild:formula context:context];

formula = [self.yFormula xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"Y_POSITION"]];
[formulaList addChild:formula context:context];

[brick addChild:formulaList context:context];

if (self.userVariable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension SetBackgroundAndWaitBrick: CBXMLNodeProtocol {
}
setBackgroundAndWaitBrick.look = CBXMLParserHelper.findLook(in: lookList as? [Any], withName: nameAttribute.stringValue())
} else {
guard let look = context.parse(from: xmlElement, withClass: Look.self as? CBXMLNodeProtocol.Type) as? Look else {
guard let look = context.parse(from: lookElement, withClass: Look.self as? CBXMLNodeProtocol.Type) as? Look else {
fatalError("Unable to parse look...")
}
lookList?.add(look)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ extension SetBackgroundBrick: CBXMLNodeProtocol {
}
setBackgroundBrick.look = CBXMLParserHelper.findLook(in: lookList as? [Any], withName: nameAttribute.stringValue())
} else {
guard let look = context.parse(from: xmlElement, withClass: Look.self as? CBXMLNodeProtocol.Type) as? Look else {
fatalError("Unable to parse look...")
}
guard let look = context.parse(from: lookElement, withClass: Look.self as? CBXMLNodeProtocol.Type) as? Look else {
fatalError("Unable to parse look...")
}
lookList?.add(look)
setBackgroundBrick.look = look
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ extension SetLookBrick: CBXMLNodeProtocol {
}
setLookBrick.look = CBXMLParserHelper.findLook(in: lookList as? [Any], withName: nameAttribute.stringValue())
} else {
guard let look = context.parse(from: xmlElement, withClass: Look.self as? CBXMLNodeProtocol.Type) as? Look else {
fatalError("Unable to parse look...")
}
guard let look = context.parse(from: lookElement, withClass: Look.self as? CBXMLNodeProtocol.Type) as? Look else {
fatalError("Unable to parse look...")
}
lookList?.add(look)
setLookBrick.look = look
}
Expand All @@ -66,6 +66,7 @@ extension SetLookBrick: CBXMLNodeProtocol {
let refPath = CBXMLSerializerHelper.relativeXPath(to: self.look, inLookList: context.spriteObject.lookList as? [Any], withDepth: depthOfResource)

referenceXMLElement?.addAttribute(GDataXMLElement.attribute(withName: "reference", escapedStringValue: refPath) as? GDataXMLNode)

brick?.addChild(referenceXMLElement, context: context)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ - (GDataXMLElement*)xmlElementWithContext:(CBXMLSerializerContext*)context
GDataXMLElement *brick = [super xmlElementForBrickType:@"GlideToBrick" withContext:context];
GDataXMLElement *formulaList = [GDataXMLElement elementWithName:@"formulaList" context:context];

GDataXMLElement *formula = [self.yDestination xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"Y_DESTINATION"]];
GDataXMLElement *formula = [self.xDestination xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"X_DESTINATION"]];
[formulaList addChild:formula context:context];

formula = [self.xDestination xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"X_DESTINATION"]];
formula = [self.yDestination xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"Y_DESTINATION"]];
[formulaList addChild:formula context:context];

formula = [self.durationInSeconds xmlElementWithContext:context];;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ - (GDataXMLElement*)xmlElementWithContext:(CBXMLSerializerContext*)context
{
GDataXMLElement *brick = [super xmlElementForBrickType:@"PlaceAtBrick" withContext:context];
GDataXMLElement *formulaList = [GDataXMLElement elementWithName:@"formulaList" context:context];
GDataXMLElement *formula = [self.yPosition xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"Y_POSITION"]];
[formulaList addChild:formula context:context];
formula = [self.xPosition xmlElementWithContext:context];

GDataXMLElement *formula = [self.xPosition xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"X_POSITION"]];
[formulaList addChild:formula context:context];

formula = [self.yPosition xmlElementWithContext:context];
[formula addAttribute:[GDataXMLElement attributeWithName:@"category" escapedStringValue:@"Y_POSITION"]];
[formulaList addChild:formula context:context];

[brick addChild:formulaList context:context];
return brick;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ extension SetPenColorBrick: CBXMLNodeProtocol {
let brick = super.xmlElement(for: "SetPenColorBrick", with: context)
let formulaList = GDataXMLElement(name: "formulaList", context: context)

let greenFormula = self.green?.xmlElement(with: context)
greenFormula?.addAttribute(GDataXMLElement(name: "category", stringValue: "PEN_COLOR_GREEN", context: nil))
formulaList?.addChild(greenFormula, context: context)

let redFormula = self.red?.xmlElement(with: context)
redFormula?.addAttribute(GDataXMLElement(name: "category", stringValue: "PEN_COLOR_RED", context: nil))
formulaList?.addChild(redFormula, context: context)

let greenFormula = self.green?.xmlElement(with: context)
greenFormula?.addAttribute(GDataXMLElement(name: "category", stringValue: "PEN_COLOR_GREEN", context: nil))
formulaList?.addChild(greenFormula, context: context)

let blueFormula = self.blue?.xmlElement(with: context)
blueFormula?.addAttribute(GDataXMLElement(name: "category", stringValue: "PEN_COLOR_BLUE", context: nil))
formulaList?.addChild(blueFormula, context: context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,34 @@ + (instancetype)parseFromElement:(GDataXMLElement*)xmlElement withContext:(CBXML
NSString *xPath = [referenceAttribute stringValue];
soundElement = [soundElement singleNodeForCatrobatXPath:xPath];
[XMLError exceptionIfNil:soundElement message:@"Invalid reference in PlaySoundAndWaitBrick. No or too many sounds found!"];
GDataXMLNode *nameElement = [soundElement childWithElementName:@"name"];
[XMLError exceptionIfNil:nameElement message:@"Sound element does not contain a name child element!"];

GDataXMLNode *nameElement = nil;
if([context isGreaterThanLanguageVersion:0.995])
{
nameElement = [soundElement attributeForName:@"name"];
} else
{
nameElement = [soundElement childWithElementName:@"name"];
}
[XMLError exceptionIfNil:nameElement message:@"Sound name not present"];

sound = [CBXMLParserHelper findSoundInArray:soundList withName:[nameElement stringValue]];
[XMLError exceptionIfNil:sound message:@"Fatal error: no sound found in list, but should already exist!"];
} else {
GDataXMLElement *soundElement = [xmlElement childWithElementName:@"sound"];
[XMLError exceptionIfNil:soundElement message:@"sound element not present"];

GDataXMLElement *soundName = [soundElement childWithElementName:@"name"];
[XMLError exceptionIfNil:soundName message:@"Sound name not present"];
GDataXMLNode *soundName = nil;

if([context isGreaterThanLanguageVersion:0.995])
{
soundName = [soundElement attributeForName:@"name"];
} else
{
soundName = [soundElement childWithElementName:@"name"];
}
[XMLError exceptionIfNil:soundName message:@"Sound name not present"];

sound = [CBXMLParserHelper findSoundInArray:soundList withName:[soundName stringValue]];

if (sound == nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,35 @@ + (instancetype)parseFromElement:(GDataXMLElement*)xmlElement withContext:(CBXML
NSString *xPath = [referenceAttribute stringValue];
soundElement = [soundElement singleNodeForCatrobatXPath:xPath];
[XMLError exceptionIfNil:soundElement message:@"Invalid reference in PlaySoundBrick. No or too many sounds found!"];
GDataXMLNode *nameElement = [soundElement childWithElementName:@"name"];
[XMLError exceptionIfNil:nameElement message:@"Sound element does not contain a name child element!"];

GDataXMLNode *nameElement = nil;

if([context isGreaterThanLanguageVersion:0.995])
{
nameElement = [soundElement attributeForName:@"name"];
} else
{
nameElement = [soundElement childWithElementName:@"name"];
}
[XMLError exceptionIfNil:nameElement message:@"Sound name not present"];

sound = [CBXMLParserHelper findSoundInArray:soundList withName:[nameElement stringValue]];
[XMLError exceptionIfNil:sound message:@"Fatal error: no sound found in list, but should already exist!"];
} else {
// OMG!! a sound has been defined within the brick element...
GDataXMLElement *soundElement = [xmlElement childWithElementName:@"sound"];
[XMLError exceptionIfNil:soundElement message:@"sound element not present"];

GDataXMLElement *soundName = [soundElement childWithElementName:@"name"];
[XMLError exceptionIfNil:soundName message:@"Sound name not present"];
GDataXMLNode *soundName = nil;

if([context isGreaterThanLanguageVersion:0.995])
{
soundName = [soundElement attributeForName:@"name"];
} else
{
soundName = [soundElement childWithElementName:@"name"];
}
[XMLError exceptionIfNil:soundName message:@"Sound name not present"];

sound = [CBXMLParserHelper findSoundInArray:soundList withName:[soundName stringValue]];

if (sound == nil) {
Expand Down
29 changes: 21 additions & 8 deletions src/Catty/XML/XMLHandler/Look/Look+CBXMLHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@ + (instancetype)parseFromElement:(GDataXMLElement*)xmlElement withContext:(CBXML
GDataXMLNode *nameAttribute = [xmlElement attributeForName:@"name"];
[XMLError exceptionIfNil:nameAttribute message:@"Look must contain a name attribute"];
NSArray *lookChildElements = [xmlElement children];
[XMLError exceptionIf:[lookChildElements count] notEquals:1
message:@"Look must contain a filename child node"];
GDataXMLNode *fileNameElement = [lookChildElements firstObject];
[XMLError exceptionIfString:fileNameElement.name isNotEqualToString:@"fileName"
message:@"Look contains wrong child node"];
Look *look = [[Look alloc] initWithName:[nameAttribute stringValue] andPath:[fileNameElement stringValue]];
GDataXMLNode *fileNameAttribute = nil;

if([context isGreaterThanLanguageVersion:0.995])
{
fileNameAttribute = [xmlElement attributeForName:@"fileName"];
[XMLError exceptionIfNil:fileNameAttribute message:@"Look must contain a fileName attribute"];
} else {
[XMLError exceptionIf:[lookChildElements count] notEquals:1
message:@"Look must contain a filename child node"];
fileNameAttribute = [lookChildElements firstObject];
[XMLError exceptionIfString:fileNameAttribute.name isNotEqualToString:@"fileName"
message:@"Look contains wrong child node"];
}

Look *look = [[Look alloc] initWithName:[nameAttribute stringValue] andPath:[fileNameAttribute stringValue]];
return look;
}

Expand All @@ -51,9 +60,13 @@ - (GDataXMLElement*)xmlElementWithContext:(CBXMLSerializerContext*)context
{
NSUInteger indexOfLook = [CBXMLSerializerHelper indexOfElement:self inArray:context.spriteObject.lookList];
GDataXMLElement *xmlElement = [GDataXMLElement elementWithName:@"look" xPathIndex:(indexOfLook+1) context:context];

[xmlElement addAttribute:[GDataXMLElement attributeWithName:@"fileName" escapedStringValue:self.fileName]];
[xmlElement addAttribute:[GDataXMLElement attributeWithName:@"name" escapedStringValue:self.name]];
[xmlElement addChild:[GDataXMLElement elementWithName:@"fileName" stringValue:self.fileName
context:context] context:context];

//[xmlElement addAttribute:[GDataXMLElement attributeWithName:@"fileName" escapedStringValue:self.fileName]];
//[xmlElement addAttribute:[GDataXMLElement attributeWithName:@"name" escapedStringValue:self.name]];

return xmlElement;
}

Expand Down
8 changes: 0 additions & 8 deletions src/Catty/XML/XMLHandler/Scene/Scene+CBXMLHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,6 @@ - (GDataXMLElement *)xmlElementWithContext:(CBXMLSerializerContext *)context {
GDataXMLElement *userDataXmlElement = [userData serializeForObject:context];
[sceneXmlElement addChild:userDataXmlElement context:context];

[XMLError exceptionIfNil:self.width message:@"Original Width not present"];
GDataXMLElement *originalWidth = [GDataXMLElement elementWithName:@"originalWidth" stringValue:self.width context:context];
[sceneXmlElement addChild:originalWidth context:context];

[XMLError exceptionIfNil:self.height message:@"Original Hight not present"];
GDataXMLElement *originalHeight = [GDataXMLElement elementWithName:@"originalHeight" stringValue:self.height context:context];
[sceneXmlElement addChild:originalHeight context:context];

return sceneXmlElement;
}

Expand Down
Loading

0 comments on commit dce344b

Please sign in to comment.